Giter Site home page Giter Site logo

Comments (2)

enawork avatar enawork commented on June 9, 2024

After some trial and error, I managed to create the following React wrapper for my project, and by importing @viselect/vanilla, I was able to workaround the issue mentioned above.

SelectionArea.js

/* eslint-disable no-use-before-define */
import VanillaSelectionArea from '@viselect/vanilla'
import React from 'react'

export default function SelectionArea (props) {
  const { onBeforeStart, onBeforeDrag, onStart, onMove, onStop, ...opt } = props
  const root = React.createRef()

  const onBeforeStartRef = React.useRef()
  const onBeforeDragRef = React.useRef()
  const onStartRef = React.useRef()
  const onMoveRef = React.useRef()
  const onStopRef = React.useRef()

  React.useEffect(() => {
    onBeforeStartRef.current = onBeforeStart
    onBeforeDragRef.current = onBeforeDrag
    onStartRef.current = onStart
    onMoveRef.current = onMove
    onStopRef.current = onStop
  })

  React.useEffect(() => {
    const areaBoundaries = root.current

    const selection = new VanillaSelectionArea({
      boundaries: areaBoundaries,
      ...opt
    })

    onBeforeStart && selection.on('beforeStart', (event) => {
      if (onBeforeStartRef.current) {
        onBeforeStartRef.current(event)
      }
    })
    onBeforeDrag && selection.on('beforedrag', (event) => {
      if (onBeforeDragRef.current) {
        onBeforeDragRef.current(event)
      }
    })
    onStart && selection.on('start', (event) => {
      if (onStartRef.current) {
        onStartRef.current(event)
      }
    })
    onMove && selection.on('move', (event) => {
      if (onMoveRef.current) {
        onMoveRef.current(event)
      }
    })
    onStop && selection.on('stop', (event) => {
      if (onStopRef.current) {
        onStopRef.current(event)
      }
    })

    return () => selection.destroy()
  }, [])

  return (
    React.createElement('div', { ref: root, className: props.className, id: props.id }, props.children)
  )
}

I believe it would look like this in TypeScript:
https://gist.github.com/enawork/84c0d88fb2e34ed232dbeeb603fedd9c#file-selectionarea-tsx

from selection.

simonwep avatar simonwep commented on June 9, 2024

Thanks for the elaborative feature request, I'll label this as a bug since I would expect this to be reactive either way... I'll take a look at it :)

from selection.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.