Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow boundaries to be passed as a prop to SelectionArea component: #220

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/react/src/SelectionArea.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-use-before-define */
import VanillaSelectionArea from '@viselect/vanilla';
import {SelectionEvents, SelectionOptions} from '@viselect/vanilla';
import React, {createRef, useEffect, createContext, useContext, useState} from 'react';
import React, {useEffect, createContext, useContext, useRef, useState} from 'react';

export interface SelectionAreaProps extends Omit<Partial<SelectionOptions>, 'boundaries'>, React.HTMLAttributes<HTMLDivElement> {
export interface SelectionAreaProps extends Partial<SelectionOptions>, React.HTMLAttributes<HTMLDivElement> {
id?: string;
className?: string;
onBeforeStart?: SelectionEvents['beforestart'];
Expand All @@ -19,12 +19,12 @@ export const useSelection = () => useContext(SelectionContext);

export const SelectionArea: React.FunctionComponent<SelectionAreaProps> = props => {
const [selectionState, setSelection] = useState<VanillaSelectionArea | undefined>(undefined);
const root = createRef<HTMLDivElement>();
const root = useRef<HTMLDivElement>(null);

useEffect(() => {
/* eslint-disable @typescript-eslint/no-unused-vars */
const {onBeforeStart, onBeforeDrag, onStart, onMove, onStop, ...opt} = props;
const areaBoundaries = root.current as HTMLElement;
const {boundaries, onBeforeStart, onBeforeDrag, onStart, onMove, onStop, ...opt} = props;
const areaBoundaries = boundaries ? boundaries : (root.current as HTMLElement);

const selection = new VanillaSelectionArea({
boundaries: areaBoundaries,
Expand All @@ -47,9 +47,13 @@ export const SelectionArea: React.FunctionComponent<SelectionAreaProps> = props

return (
<SelectionContext.Provider value={selectionState}>
<div ref={root} className={props.className} id={props.id}>
{props.children}
</div>
{props.boundaries ? (
props.children
) : (
<div ref={root} className={props.className} id={props.id}>
{props.children}
</div>
)}
</SelectionContext.Provider>
);
};