diff --git a/src/components/Dropdown.tsx b/src/components/Dropdown.tsx new file mode 100644 index 00000000..91f01ded --- /dev/null +++ b/src/components/Dropdown.tsx @@ -0,0 +1,129 @@ +import { Icon, Icons } from "components/Icon"; +import React, { + MouseEventHandler, + SyntheticEvent, + useEffect, + useState, +} from "react"; + +import { Backdrop, useBackdrop } from "components/layout/Backdrop"; +import { ButtonControl } from "./buttons/ButtonControl"; + +export interface OptionItem { + id: string; + name: string; +} + +interface DropdownProps { + open: boolean; + setOpen: React.Dispatch>; + selectedItem: string; + setSelectedItem: (value: string) => void; + options: Array; +} + +export interface OptionProps { + option: OptionItem; + onClick: MouseEventHandler; + tabIndex?: number; +} + +function Option({ option, onClick, tabIndex }: OptionProps) { + return ( +
+ + +
+ ); +} + +export const Dropdown = React.forwardRef( + (props: DropdownProps, ref) => { + const [setBackdrop, backdropProps, highlightedProps] = useBackdrop(); + const [delayedSelectedId, setDelayedSelectedId] = useState( + props.selectedItem + ); + + useEffect(() => { + let id: NodeJS.Timeout; + + if (props.open) { + setDelayedSelectedId(props.selectedItem); + } else { + id = setTimeout(() => { + setDelayedSelectedId(props.selectedItem); + }, 200); + } + return () => { + if (id) clearTimeout(id); + }; + /* eslint-disable-next-line */ + }, [props.open]); + + const selectedItem: OptionItem = + props.options.find((opt) => opt.id === props.selectedItem) || + props.options[0]; + + useEffect(() => { + setBackdrop(props.open); + /* eslint-disable-next-line */ + }, [props.open]); + + const onOptionClick = (e: SyntheticEvent, option: OptionItem) => { + e.stopPropagation(); + props.setSelectedItem(option.id); + props.setOpen(false); + }; + + return ( +
props.setOpen((open) => !open)} + > +
+ + {selectedItem.name} + + +
+ {props.options + .filter((opt) => opt.id !== delayedSelectedId) + .map((opt) => ( +
+
+ props.setOpen(false)} {...backdropProps} /> +
+ ); + } +); diff --git a/src/state/watched/store.ts b/src/state/watched/store.ts index 108c1c6f..25bd42bc 100644 --- a/src/state/watched/store.ts +++ b/src/state/watched/store.ts @@ -1,4 +1,4 @@ -import { versionedStoreBuilder } from 'utils/storage'; +import { versionedStoreBuilder } from "utils/storage"; /* version 0 @@ -33,19 +33,19 @@ version 0 */ export const VideoProgressStore = versionedStoreBuilder() - .setKey('video-progress') - .addVersion({ - version: 0, - }) - .addVersion({ - version: 1, - migrate(data: any) { - // TODO migration - }, - create() { - return { - items: [], - } - } - }) - .build() + .setKey("video-progress") + .addVersion({ + version: 0, + }) + .addVersion({ + version: 1, + migrate(data: any) { + // TODO migration + }, + create() { + return { + items: [], + }; + }, + }) + .build();