diff --git a/gui/src/components/commons/Dropdown.tsx b/gui/src/components/commons/Dropdown.tsx index b550375d0..4777f091f 100644 --- a/gui/src/components/commons/Dropdown.tsx +++ b/gui/src/components/commons/Dropdown.tsx @@ -1,272 +1,434 @@ import classNames from 'classnames'; -import { ReactNode, useEffect, useLayoutEffect, useRef, useState } from 'react'; +import { + forwardRef, + ReactNode, + useEffect, + useLayoutEffect, + useRef, + useState, +} from 'react'; import { Control, - Controller, FieldError, + useController, UseControllerProps, } from 'react-hook-form'; +import { ArrowDownIcon, ArrowUpIcon } from './icon/ArrowIcons'; import { a11yClick } from '@/utils/a11y'; import { createPortal } from 'react-dom'; -import { ArrowDownIcon } from './icon/ArrowIcons'; -interface DropdownProps { +type DropdownItem = { + value: string; + label: ReactNode; +}; + +export type DropdownDirection = 'up' | 'down'; + +type DropdownProps = { direction?: DropdownDirection; variant?: 'primary' | 'secondary' | 'tertiary'; - alignment?: 'right' | 'left'; + alignment?: 'left' | 'right'; display?: 'fit' | 'block'; placeholder: string; name: string; items: DropdownItem[]; maxHeight?: string | number; error?: FieldError; -} - -type DropdownItemsProps = Pick< - DropdownProps, - 'direction' | 'variant' | 'alignment' | 'display' | 'items' | 'maxHeight' -> & { - onSelectItem: (item: DropdownItem) => void; - onBackdropClick: () => void; - value: string; - dropdownBounds: DOMRect; }; -export interface DropdownItem { - label?: string; - component?: ReactNode; - value: string; - fontName?: string; -} - -export type DropdownDirection = 'up' | 'down'; - -export function DropdownItems({ - display, - direction, +function DropdownItem({ + item, variant, - alignment, - items, - maxHeight, + onSelected, + isOpen, value, - dropdownBounds, - onSelectItem, - onBackdropClick, -}: DropdownItemsProps) { - const ref = useRef(null); - const [itemBounds, setItemBounds] = useState(); - - const updateBounds = () => { - if (!ref.current) return; - setItemBounds(ref.current?.getBoundingClientRect()); + innerFocusValue, + name, +}: { + item: DropdownItem; + variant: Required['variant']; + onSelected: () => void; + isOpen: boolean; + value: any; + innerFocusValue: string | null; + name: string; +}) { + const variantStyles = { + primary: + 'text-background-20 checked-hover:text-background-10 checked-hover:bg-background-50 focus:text-background-10 focus:bg-background-50', + secondary: + 'text-background-20 checked-hover:text-background-10 checked-hover:bg-background-60 focus:text-background-10 focus:bg-background-60', + tertiary: + 'bg-accent-background-30 checked-hover:bg-accent-background-20 focus:bg-accent-background-20', }; - useLayoutEffect(() => { - updateBounds(); - }, []); + const ref = useRef(null); - const GAP = 8; + useLayoutEffect(() => { + if (!innerFocusValue) { + return; + } + if (innerFocusValue === item.value) { + ref.current?.scrollIntoView({ block: 'nearest' }); + } + }, [innerFocusValue]); + + useLayoutEffect(() => { + if (!isOpen) { + return; + } + + if ( + innerFocusValue === item.value || + (!innerFocusValue && item.value === value) + ) { + ref.current?.scrollIntoView({ block: 'nearest' }); + } + }, [isOpen]); return ( - <> -
-
-
    - {items.map((item) => ( -
  • { - onSelectItem(item); - }} - onKeyDown={(ev) => { - if (!a11yClick(ev)) return; - onSelectItem(item); - }} - key={item.value} - tabIndex={0} - data-checked={item.value === value} - > - {item.component || item.label} -
  • - ))} -
-
- +
a11yClick(e) && onSelected()} + tabIndex={-1} + aria-hidden={!isOpen} + data-checked={item.value === value} + ref={ref} + id={`__dropdownList-${name}-item-${item.value}`} + > + {item.label} +
); } +type DropdownListProps = { + isOpen: boolean; + onSelect: (item: DropdownItem) => void; + value: any; + innerFocusValue: string | null; +} & Pick< + Required, + | 'display' + | 'alignment' + | 'direction' + | 'items' + | 'variant' + | 'maxHeight' + | 'name' +>; + +const DropdownList = forwardRef(function ( + { + isOpen, + onSelect, + value, + innerFocusValue, + display, + alignment, + direction, + items, + variant, + maxHeight, + name, + }, + ref +) { + const variantStyles = { + primary: 'bg-background-60', + secondary: 'bg-background-70', + tertiary: 'bg-accent-background-30', + }; + + const getDisplayStyle = () => { + if (display === 'block') { + return { + left: 'var(--dropdown-field-left)', + right: 'var(--dropdown-field-right)', + }; + } + return alignment === 'left' + ? { left: 'var(--dropdown-field-left)' } + : { right: 'var(--dropdown-field-right)' }; + }; + + const directionStyles = { + up: { + bottom: 'calc(var(--dropdown-field-top) + 0.75rem)', + }, + down: { + top: 'calc(var(--dropdown-field-bottom) + 0.75rem)', + }, + }; + + return ( +
{ + if (!isOpen) { + (e.target as HTMLDivElement).scrollTo({ top: 0 }); + } + }} + ref={ref} + id={`__dropdownList-${name}`} + > +
    + {items.map((item) => ( + onSelect(item)} + isOpen={isOpen} + key={item.value} + value={value} + innerFocusValue={innerFocusValue} + name={name} + /> + ))} +
+
+ ); +}); + export function DropdownInside({ direction = 'up', variant = 'primary', alignment = 'right', display = 'fit', - maxHeight = '50vh', placeholder, - items = [], + name, + items, + maxHeight = '50vh', value, onChange, error, -}: DropdownProps & { - name: string; - value: string; - onChange: (v: string) => void; -}) { - const ref = useRef(null); - const [isOpen, setOpenState] = useState(false); - const [dropdownBounds, setDropdownBounds] = useState(); +}: DropdownProps & { value: string; onChange: (value: string) => void }) { + const [isOpen, setIsOpen] = useState(false); - const updateBounds = () => { - if (!ref.current) return; - setDropdownBounds(ref.current?.getBoundingClientRect()); + useLayoutEffect(() => { + ref.current?.focus(); + }, [value]); + + const variantStyles = { + primary: 'bg-background-60 hover:bg-background-50', + secondary: 'bg-background-70 hover:bg-background-60', + tertiary: 'bg-accent-background-30 hover:bg-accent-background-20', }; - const onResize = () => { - // We could have two behaviours here: - // 1 - We update the bounds of the dropdown so the size and position match. - // Works but have a slight delay when resizing, kinda looks laggy - // 2 - We close the dropdown on resize. - // We could consider this as the same as clicking outside of the dropdown - // This is the approach choosen RN - setOpen(false); + const displayStyles = { + fit: 'w-fit', + block: 'w-full', }; + const getShownValue = (value: any) => + value + ? (items.find((item) => item.value === value)?.label ?? placeholder) + : placeholder; + + const ref = useRef(null); + const listRef = useRef(null); + + const [innerFocusIndex, setInnerFocusIndex] = useState(null); + const getCurrentActiveIndex = () => { + return items.findIndex((item) => item.value === value); + }; + const innerFocusPrev = () => { + const current = innerFocusIndex ?? getCurrentActiveIndex(); + + setInnerFocusIndex(current > 0 ? current - 1 : current); + }; + const innerFocusNext = () => { + const current = innerFocusIndex ?? getCurrentActiveIndex(); + + setInnerFocusIndex(current < items.length - 1 ? current + 1 : current); + }; + + const updateFieldBoundingRect = () => { + if (!ref.current || !listRef.current) { + return; + } + const boundingRect = ref.current.getBoundingClientRect(); + + const left = boundingRect.left; + const right = window.innerWidth - boundingRect.right; + const top = window.innerHeight - boundingRect.top; + const bottom = boundingRect.bottom; + listRef.current?.style.setProperty('--dropdown-field-left', `${left}px`); + listRef.current?.style.setProperty('--dropdown-field-right', `${right}px`); + listRef.current?.style.setProperty('--dropdown-field-top', `${top}px`); + listRef.current?.style.setProperty( + '--dropdown-field-bottom', + `${bottom}px` + ); + }; + useLayoutEffect(updateFieldBoundingRect, [ref.current]); + window.addEventListener('scroll', updateFieldBoundingRect, true); + window.addEventListener('resize', updateFieldBoundingRect, true); + useEffect(() => { - window.addEventListener('resize', onResize); - return () => { - window.removeEventListener('resize', onResize); - }; - }, []); + if (!isOpen) { + setInnerFocusIndex(null); + } + }, [isOpen]); - const setOpen = (open: boolean | ((prevState: boolean) => boolean)) => { - updateBounds(); - setOpenState(open); - }; return ( <> -
+
setIsOpen(!isOpen)} + onKeyDown={(e) => { + if (!isOpen) { + if (a11yClick(e)) { + setInnerFocusIndex( + items.findIndex((item) => item.value === value) + ); + setIsOpen(!isOpen); + e.preventDefault(); + return; + } + + if (e.key === 'ArrowDown') { + setInnerFocusIndex(0); + setIsOpen(true); + e.preventDefault(); + return; + } + + if (e.key === 'ArrowUp') { + setInnerFocusIndex(items.length - 1); + setIsOpen(true); + e.preventDefault(); + return; + } + } else { + if (a11yClick(e)) { + e.preventDefault(); + if (innerFocusIndex === null) { + setIsOpen(false); + return; + } + + onChange(items[innerFocusIndex].value); + setIsOpen(false); + } + switch (e.key) { + case 'ArrowUp': + innerFocusPrev(); + e.preventDefault(); + return; + case 'ArrowDown': + innerFocusNext(); + e.preventDefault(); + return; + case 'Escape': + setIsOpen(false); + return; + case 'Home': + setInnerFocusIndex(0); + return; + case 'End': + setInnerFocusIndex(items.length - 1); + return; + } + } + }} + onBlur={(e) => { + if ( + e.currentTarget.contains(e.relatedTarget) || + listRef?.current?.contains(e.relatedTarget) + ) { + return; + } + + setIsOpen(false); + }} + >
setOpen((open) => !open)} - onKeyDown={(ev) => a11yClick(ev) && setOpen((open) => !open)} tabIndex={0} + ref={ref} + aria-controls={`__dropdownList-${name}`} + aria-activedescendant={ + innerFocusIndex === null + ? '' + : `__dropdownList-${name}-item-${items[innerFocusIndex].value}` + } + role="combobox" > -
- {items.find((i) => i.value == value)?.component || - items.find((i) => i.value == value)?.label || - placeholder} -
-
+ {direction === 'up' ? ( + + ) : ( + )} - > -
{error?.message && ( -
- {error.message} -
+
{error.message}
)} -
- {isOpen && - dropdownBounds && - createPortal( - { - setOpen(false); + isOpen={isOpen} + items={items} + onSelect={(item: DropdownItem) => { + ref.current?.focus(); onChange(item.value); }} - onBackdropClick={() => { - setOpen(false); - }} + variant={variant} + maxHeight={maxHeight} + value={value} + ref={listRef} + innerFocusValue={ + innerFocusIndex === null ? null : items[innerFocusIndex].value + } + name={name} />, document.body )} +
); } export function Dropdown({ - name, control, + name, rules, ...props }: DropdownProps & { control: Control; rules?: UseControllerProps['rules']; }) { + const { + field: { value, onChange }, + } = useController({ name, control, rules }); + return ( - ( - - )} - /> + ); } diff --git a/gui/src/components/commons/LangSelector.tsx b/gui/src/components/commons/LangSelector.tsx index 338daeecd..633fcbae4 100644 --- a/gui/src/components/commons/LangSelector.tsx +++ b/gui/src/components/commons/LangSelector.tsx @@ -23,7 +23,7 @@ export function LangSelector({ const languagesItems = useMemo( () => langs.map(({ key, name, emoji }) => ({ - component: ( + label: (
), - label: name, value: key, })), [] diff --git a/gui/src/components/onboarding/pages/trackers-assign/TrackerAssignOptions.tsx b/gui/src/components/onboarding/pages/trackers-assign/TrackerAssignOptions.tsx index b2d934108..352c73d5b 100644 --- a/gui/src/components/onboarding/pages/trackers-assign/TrackerAssignOptions.tsx +++ b/gui/src/components/onboarding/pages/trackers-assign/TrackerAssignOptions.tsx @@ -97,7 +97,7 @@ export function TrackerAssignOptions({ placeholder={''} items={Object.entries(ASSIGN_MODE_OPTIONS).map( ([mode, trackersCount]) => ({ - component: ( + label: (
diff --git a/gui/src/components/settings/pages/InterfaceSettings.tsx b/gui/src/components/settings/pages/InterfaceSettings.tsx index dc57954fe..67a780270 100644 --- a/gui/src/components/settings/pages/InterfaceSettings.tsx +++ b/gui/src/components/settings/pages/InterfaceSettings.tsx @@ -79,6 +79,30 @@ export function InterfaceSettings() { }, }); + const fontOptions = [ + { + label: l10n.getString('settings-interface-appearance-font-slime_font'), + value: 'poppins', + fontName: 'poppins, Noto Sans CJK', + }, + { + label: 'OpenDyslexic', + value: 'OpenDyslexic', + fontName: 'OpenDyslexic', + }, + { label: 'Lexend', value: 'Lexend', fontName: 'Lexend' }, + { label: 'Ubuntu', value: 'Ubuntu', fontName: 'Ubuntu' }, + { + label: 'Noto Sans (CJK)', + value: 'Noto Sans CJK', + fontName: 'Noto Sans CJK', + }, + { + label: l10n.getString('settings-interface-appearance-font-os_font'), + value: 'ui-sans-serif', + }, + ]; + const onSubmit = (values: InterfaceSettingsForm) => { setConfig({ watchNewDevices: values.notifications.watchNewDevices, @@ -485,33 +509,14 @@ export function InterfaceSettings() { 'settings-interface-appearance-font-placeholder' )} /* Supports multiple items by separating them with a comma */ - items={[ - { - label: l10n.getString( - 'settings-interface-appearance-font-slime_font' - ), - value: 'poppins', - fontName: 'poppins, Noto Sans CJK', - }, - { - label: 'OpenDyslexic', - value: 'OpenDyslexic', - fontName: 'OpenDyslexic', - }, - { label: 'Lexend', value: 'Lexend', fontName: 'Lexend' }, - { label: 'Ubuntu', value: 'Ubuntu', fontName: 'Ubuntu' }, - { - label: 'Noto Sans (CJK)', - value: 'Noto Sans CJK', - fontName: 'Noto Sans CJK', - }, - { - label: l10n.getString( - 'settings-interface-appearance-font-os_font' - ), - value: 'ui-sans-serif', - }, - ]} + items={fontOptions.map((option) => ({ + label: ( + + {option.label} + + ), + value: option.value, + }))} alignment="left" />