Skip to content

Commit 13b33f7

Browse files
committed
refactor: minor simplifications in react, preact and vue packages
1 parent 7b4dd98 commit 13b33f7

File tree

5 files changed

+22
-25
lines changed

5 files changed

+22
-25
lines changed

packages/preact/src/SelectionArea.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ export interface SelectionAreaProps extends PartialSelectionOptions, JSX.HTMLAtt
1313
onStop?: SelectionEvents['stop'];
1414
}
1515

16-
const SelectionContext = createContext<VanillaSelectionArea | undefined>(undefined);
16+
const SelectionContext = createContext<VanillaSelectionArea | undefined>(undefined);
1717

1818
export const useSelection = () => useContext(SelectionContext);
1919

2020
export const SelectionArea: FunctionalComponent<SelectionAreaProps> = props => {
21-
const [selectionState, setSelection] = useState<VanillaSelectionArea | undefined>(undefined);
21+
const [instance, setInstance] = useState<VanillaSelectionArea | undefined>(undefined);
2222
const root = createRef<HTMLDivElement>();
2323

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

2928
const selection = new VanillaSelectionArea({
30-
boundaries: areaBoundaries,
29+
boundaries: boundaries as HTMLElement,
3130
...opt
3231
});
3332

@@ -37,16 +36,16 @@ export const SelectionArea: FunctionalComponent<SelectionAreaProps> = props => {
3736
selection.on('move', evt => props.onMove?.(evt));
3837
selection.on('stop', evt => props.onStop?.(evt));
3938

40-
setSelection(selection);
39+
setInstance(selection);
4140

4241
return () => {
4342
selection.destroy();
44-
setSelection(undefined);
43+
setInstance(undefined);
4544
};
4645
}, []);
4746

4847
return (
49-
<SelectionContext.Provider value={selectionState}>
48+
<SelectionContext.Provider value={instance}>
5049
{props.boundaries ? (
5150
props.children
5251
) : (

packages/react/src/SelectionArea.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@ export interface SelectionAreaProps extends PartialSelectionOptions, React.HTMLA
1212
onStop?: SelectionEvents['stop'];
1313
}
1414

15-
const SelectionContext = createContext<VanillaSelectionArea | undefined>(undefined);
15+
const SelectionContext = createContext<VanillaSelectionArea | undefined>(undefined);
1616

1717
export const useSelection = () => useContext(SelectionContext);
1818

1919
export const SelectionArea: React.FunctionComponent<SelectionAreaProps> = props => {
20-
const [selectionState, setSelection] = useState<VanillaSelectionArea | undefined>(undefined);
20+
const [instance, setInstance] = useState<VanillaSelectionArea | undefined>(undefined);
2121
const root = useRef<HTMLDivElement>(null);
2222

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

2827
const selection = new VanillaSelectionArea({
29-
boundaries: areaBoundaries,
28+
boundaries: boundaries as HTMLElement,
3029
...opt
3130
});
3231

@@ -36,16 +35,16 @@ export const SelectionArea: React.FunctionComponent<SelectionAreaProps> = props
3635
selection.on('move', evt => props.onMove?.(evt));
3736
selection.on('stop', evt => props.onStop?.(evt));
3837

39-
setSelection(selection);
38+
setInstance(selection);
4039

4140
return () => {
4241
selection.destroy();
43-
setSelection(undefined);
42+
setInstance(undefined);
4443
};
4544
}, []);
4645

4746
return (
48-
<SelectionContext.Provider value={selectionState}>
47+
<SelectionContext.Provider value={instance}>
4948
{props.boundaries ? (
5049
props.children
5150
) : (

packages/vanilla/src/utils/intersects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type Intersection = 'center' | 'cover' | 'touch'
1+
export type Intersection = 'center' | 'cover' | 'touch';
22

33
/**
44
* Check if two DOM-Elements intersects each other.

packages/vanilla/src/utils/matchesTrigger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ export type Modifier = 'ctrl'
1010
| 'alt'
1111
| 'shift';
1212

13-
export type Trigger = MouseButton | MouseButtonWithModifiers;
14-
1513
export type MouseButtonWithModifiers = {
1614
button: MouseButton,
1715
modifiers: Modifier[]
1816
};
1917

18+
export type Trigger = MouseButton | MouseButtonWithModifiers;
19+
2020
/**
2121
* Determines whether a MouseEvent should execute until completion depending on
2222
* which button and modifier(s) are active for the MouseEvent.

packages/vue/src/SelectionArea.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<script lang="ts" setup>
88
import SelectionArea, {SelectionEvent, PartialSelectionOptions} from '@viselect/vanilla';
9-
import {onBeforeUnmount, ref, watchEffect, shallowRef} from 'vue';
9+
import {onBeforeUnmount, shallowRef, useTemplateRef, watch} from 'vue';
1010
1111
const emit = defineEmits<{
1212
(e: 'before-start', v: SelectionEvent): void;
@@ -21,14 +21,14 @@ const props = defineProps<{
2121
options: Omit<PartialSelectionOptions, 'boundaries'>;
2222
}>();
2323
24-
const container = ref<HTMLDivElement>();
24+
const container = useTemplateRef('container');
2525
const instance = shallowRef<SelectionArea | undefined>();
2626
27-
watchEffect(() => {
28-
if (container.value) {
27+
watch(container, (element) => {
28+
if (element) {
2929
instance.value?.destroy();
3030
instance.value = new SelectionArea({
31-
boundaries: container.value,
31+
boundaries: element,
3232
...props.options
3333
});
3434
@@ -42,7 +42,6 @@ watchEffect(() => {
4242
}
4343
});
4444
45-
4645
onBeforeUnmount(() => {
4746
instance.value?.destroy();
4847
});

0 commit comments

Comments
 (0)