Skip to content

Commit

Permalink
refactor: minor simplifications in react, preact and vue packages
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwep committed Jan 19, 2025
1 parent 7b4dd98 commit 13b33f7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
15 changes: 7 additions & 8 deletions packages/preact/src/SelectionArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ export interface SelectionAreaProps extends PartialSelectionOptions, JSX.HTMLAtt
onStop?: SelectionEvents['stop'];
}

const SelectionContext = createContext<VanillaSelectionArea | undefined>(undefined);
const SelectionContext = createContext<VanillaSelectionArea | undefined>(undefined);

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

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

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

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

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

setSelection(selection);
setInstance(selection);

return () => {
selection.destroy();
setSelection(undefined);
setInstance(undefined);
};
}, []);

return (
<SelectionContext.Provider value={selectionState}>
<SelectionContext.Provider value={instance}>
{props.boundaries ? (
props.children
) : (
Expand Down
15 changes: 7 additions & 8 deletions packages/react/src/SelectionArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ export interface SelectionAreaProps extends PartialSelectionOptions, React.HTMLA
onStop?: SelectionEvents['stop'];
}

const SelectionContext = createContext<VanillaSelectionArea | undefined>(undefined);
const SelectionContext = createContext<VanillaSelectionArea | undefined>(undefined);

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

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

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

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

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

setSelection(selection);
setInstance(selection);

return () => {
selection.destroy();
setSelection(undefined);
setInstance(undefined);
};
}, []);

return (
<SelectionContext.Provider value={selectionState}>
<SelectionContext.Provider value={instance}>
{props.boundaries ? (
props.children
) : (
Expand Down
2 changes: 1 addition & 1 deletion packages/vanilla/src/utils/intersects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Intersection = 'center' | 'cover' | 'touch'
export type Intersection = 'center' | 'cover' | 'touch';

/**
* Check if two DOM-Elements intersects each other.
Expand Down
4 changes: 2 additions & 2 deletions packages/vanilla/src/utils/matchesTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export type Modifier = 'ctrl'
| 'alt'
| 'shift';

export type Trigger = MouseButton | MouseButtonWithModifiers;

export type MouseButtonWithModifiers = {
button: MouseButton,
modifiers: Modifier[]
};

export type Trigger = MouseButton | MouseButtonWithModifiers;

/**
* Determines whether a MouseEvent should execute until completion depending on
* which button and modifier(s) are active for the MouseEvent.
Expand Down
11 changes: 5 additions & 6 deletions packages/vue/src/SelectionArea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script lang="ts" setup>
import SelectionArea, {SelectionEvent, PartialSelectionOptions} from '@viselect/vanilla';
import {onBeforeUnmount, ref, watchEffect, shallowRef} from 'vue';
import {onBeforeUnmount, shallowRef, useTemplateRef, watch} from 'vue';
const emit = defineEmits<{
(e: 'before-start', v: SelectionEvent): void;
Expand All @@ -21,14 +21,14 @@ const props = defineProps<{
options: Omit<PartialSelectionOptions, 'boundaries'>;
}>();
const container = ref<HTMLDivElement>();
const container = useTemplateRef('container');
const instance = shallowRef<SelectionArea | undefined>();
watchEffect(() => {
if (container.value) {
watch(container, (element) => {
if (element) {
instance.value?.destroy();
instance.value = new SelectionArea({
boundaries: container.value,
boundaries: element,
...props.options
});
Expand All @@ -42,7 +42,6 @@ watchEffect(() => {
}
});
onBeforeUnmount(() => {
instance.value?.destroy();
});
Expand Down

0 comments on commit 13b33f7

Please sign in to comment.