Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fonts manager without combobox
Oleg Isonenac4738590905
diff --git a/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx b/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsxindex 796c13daa9..f749419545 100644--- a/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx+++ b/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx@@ -1,15 +1,22 @@import type { Asset } from "@webstudio-is/asset-uploader";import {Flex,- useCombobox,Separator,SearchField,+ List,+ ListItem,} from "@webstudio-is/design-system";import { AssetUpload, PreviewAsset, useAssets } from "~/designer/shared/assets";import { SYSTEM_FONTS } from "@webstudio-is/fonts";-import { Fragment, useCallback, useEffect, useMemo, useState } from "react";+import {+ type KeyboardEventHandler,+ type ChangeEventHandler,+ useMemo,+ useState,+} from "react";+import { matchSorter } from "match-sorter";import { ItemMenu } from "./item-menu";-import { Listbox, ListboxItem } from "./listbox";+import { CheckIcon } from "@webstudio-is/icons";type Item = {label: string;@@ -43,15 +50,9 @@ const NotFound = () => {);};-const useLogic = ({- value,- onChange,-}: {- value: string;- onChange: (value: string) => void;-}) => {- const [search, setSearch] = useState("");+const useLogic = ({ onChange }: { onChange: (value: string) => void }) => {const { assets, handleDelete } = useAssets("font");+ const [selectedItemIndex, setSelectedItemIndex] = useState(-1);const handleDeleteByLabel = (family: string) => {// One family may have multiple assets for different formats, so we need to delete them all.const ids = assets@@ -67,32 +68,7 @@ const useLogic = ({};const fontItems = useMemo(() => toItems(assets), [assets]);-- const selectedItem =- fontItems.find((item) => item.label === value) ?? fontItems[0];-- const {- items: filteredItems,- resetFilter,- highlightedIndex,- isOpen,- inputValue,- ...comboboxProps- } = useCombobox({- // Makes sure first keydown selectsthe item instead of opening the list first otherwise.- isOpen: true,- items: fontItems,- value: selectedItem,- itemToString(item) {- return item?.label ?? "";- },- onItemSelect(value) {- if (value !== null) {- onChange(value.label);- handleCancelSearch();- }- },- });+ const [filteredItems, setFilteredItems] = useState(fontItems);const { uploadedItems, systemItems } = useMemo(() => {const uploadedItems = filteredItems.filter(@@ -102,25 +78,63 @@ const useLogic = ({return { uploadedItems, systemItems };}, [filteredItems]);- useEffect(() => {- setSearch(inputValue);- }, [inputValue]);+ const handleCancelSearch = () => {+ setFilteredItems(fontItems);+ };- const handleCancelSearch = useCallback(() => {- resetFilter();- setSearch("");- }, [resetFilter]);+ const handleSelectItem = (indexOrDirection: number | "next" | "previous") => {+ const nextIndex =+ indexOrDirection === "next"+ ? selectedItemIndex + 1+ : indexOrDirection === "previous"+ ? selectedItemIndex - 1+ : indexOrDirection;- useEffect(handleCancelSearch, [fontItems, selectedItem, handleCancelSearch]);+ if (nextIndex < 0) {+ setSelectedItemIndex(filteredItems.length - 1);+ return;+ }+ if (nextIndex >= filteredItems.length) {+ setSelectedItemIndex(0);+ return;+ }+ setSelectedItemIndex(nextIndex);+ };++ const handleKeyDown: KeyboardEventHandler = (event) => {+ if (event.code === "ArrowUp") {+ handleSelectItem("previous");+ return;+ }+ if (event.code === "ArrowDown") {+ handleSelectItem("next");+ return;+ }++ if (event.code === "Enter") {+ const item = filteredItems[selectedItemIndex];+ if (item !== undefined) onChange(item.label);+ return;+ }+ };++ const handleSearch: ChangeEventHandler<HTMLInputElement> = (event) => {+ const items = matchSorter(fontItems, event.currentTarget.value, {+ keys: [(item) => item.label],+ });+ setFilteredItems(items);+ };return {filteredItems,uploadedItems,systemItems,+ selectedItemIndex,handleDelete: handleDeleteByLabel,handleCancelSearch,- search,- ...comboboxProps,+ handleSelectItem,+ handleKeyDown,+ handleSearch,};};@@ -131,104 +145,111 @@ type FontsManagerProps = {export const FontsManager = ({ value, onChange }: FontsManagerProps) => {const {+ handleSearch,filteredItems,uploadedItems,systemItems,- getInputProps,handleDelete,handleCancelSearch,- getComboboxProps,- getMenuProps,- getItemProps,- setHighlightedIndex,- search,- } = useLogic({ value, onChange });-+ handleSelectItem,+ handleKeyDown,+ selectedItemIndex,+ } = useLogic({ onChange });return (<Flex direction="column" css={{ overflow: "hidden", py: "$1" }}><Flex css={{ py: "$2", px: "$3" }} gap="2" direction="column"><AssetUpload type="font" /><SearchField- {...getInputProps({ value: search })}autoFocusplaceholder="Search"- onBlur={undefined}- onFocus={() => {- setHighlightedIndex(-1);- }}onCancel={handleCancelSearch}+ onKeyDown={handleKeyDown}+ onChange={handleSearch}/></Flex><Separator css={{ my: "$1" }} />{filteredItems.length === 0 && <NotFound />}<Flex- {...getComboboxProps()}css={{flexDirection: "column",gap: "$3",px: "$3",}}>- <Listbox {...getMenuProps()}>+ <List onKeyDown={handleKeyDown}>{uploadedItems.length !== 0 && (- <ListboxItem disabled>{"Uploaded"}</ListboxItem>+ <ListItem state="disabled">{"Uploaded"}</ListItem>)}{uploadedItems.map((item, index) => {return (- <ListboxItem- {...getItemProps({- item,- index,- })}+ <ListItemkey={index}+ state={selectedItemIndex === index ? "selected" : undefined}+ prefix={item.label === value ? <CheckIcon /> : undefined}+ current={item.label === value}suffix={- <ItemMenu- onOpenChange={() => {- setHighlightedIndex(index);- }}- onDelete={() => {- handleDelete(item.label);- }}- onFocusTrigger={() => {- setHighlightedIndex(-1);- }}- />+ selectedItemIndex === index ? (+ <ItemMenu+ onOpenChange={() => {+ handleSelectItem(index);+ }}+ onDelete={() => {+ handleDelete(item.label);+ }}+ onFocusTrigger={() => {+ handleSelectItem(-1);+ }}+ />+ ) : undefined}onFocus={(event) => {// We need to ignore focus on a menu button insideif (event.target === event.currentTarget) {- setHighlightedIndex(index);+ handleSelectItem(index);}}}+ onMouseEnter={() => {+ handleSelectItem(index);+ }}+ onClick={() => {+ onChange(item.label);+ }}>{item.label}- </ListboxItem>+ </ListItem>);})}{systemItems.length !== 0 && (<><Separator css={{ my: "$1" }} />- <ListboxItem disabled>{"System"}</ListboxItem>+ <ListItem state="disabled">{"System"}</ListItem></>)}{systemItems.map((item, index) => {const globalIndex = uploadedItems.length + index;return (- <ListboxItem- {...getItemProps({- item,- index: globalIndex,- })}- key={index}+ <ListItem+ key={globalIndex}+ state={+ selectedItemIndex === globalIndex ? "selected" : undefined+ }+ prefix={item.label === value ? <CheckIcon /> : undefined}+ current={item.label === value}onFocus={() => {- setHighlightedIndex(globalIndex);+ handleSelectItem(globalIndex);+ }}+ onMouseEnter={() => {+ handleSelectItem(globalIndex);+ }}+ onClick={() => {+ onChange(item.label);}}>{item.label}- </ListboxItem>+ </ListItem>);})}- </Listbox>+ </List></Flex></Flex>);diff --git a/apps/designer/app/designer/shared/fonts-manager/item-menu.tsx b/apps/designer/app/designer/shared/fonts-manager/item-menu.tsxindex 5d3abc0ea4..9d749b80a5 100644--- a/apps/designer/app/designer/shared/fonts-manager/item-menu.tsx+++ b/apps/designer/app/designer/shared/fonts-manager/item-menu.tsx@@ -9,19 +9,9 @@ import {styled,} from "@webstudio-is/design-system";import { DotsHorizontalIcon } from "@webstudio-is/icons";-import { cssVars } from "@webstudio-is/css-vars";import { type FocusEventHandler, useState } from "react";-const visibilityVar = cssVars.define("visibility");--export const getItemMenuVars = (state: "visible" | "hidden") => {- return {- [visibilityVar]: state,- };-};-const MenuButton = styled(IconButton, {- visibility: cssVars.use(visibilityVar, "hidden"),color: "$hint","&:hover, &:focus": {color: "$hiContrast",diff --git a/apps/designer/app/designer/shared/fonts-manager/listbox.tsx b/apps/designer/app/designer/shared/fonts-manager/listbox.tsxdeleted file mode 100644index 78acce1ee8..0000000000--- a/apps/designer/app/designer/shared/fonts-manager/listbox.tsx+++ /dev/null@@ -1,79 +0,0 @@-import { Flex, styled, Text } from "@webstudio-is/design-system";-import { CheckIcon } from "@webstudio-is/icons";-import { ComponentProps, forwardRef } from "react";-import { getItemMenuVars } from "./item-menu";--export const Listbox = styled("ul", {- display: "flex",- flexDirection: "column",- margin: 0,- padding: 0,- variants: {- empty: {- true: {- display: "none",- },- },- },-});--const ListboxItemBase = styled("li", {- display: "grid",- gridTemplateColumns: "$4 1fr",- alignItems: "center",- justifyContent: "space-between",- height: "$5",- px: "$1",- listStyle: "none",- borderRadius: "$1",- outline: 0,- "&[data-highlighted]": {- boxShadow: "0px 0px 0px 2px $colors$blue10, 0px 0px 0px 2px $colors$blue10",- ...getItemMenuVars("visible"),- },- "&:focus-within": getItemMenuVars("visible"),- "&[disabled]": {- pointerEvents: "none",- },-});--export const ListboxItem = forwardRef<- HTMLLIElement,- ComponentProps<typeof ListboxItemBase> & {- highlighted?: boolean;- disabled?: boolean;- selected?: boolean;- suffix?: JSX.Element;- }->(- (- {- highlighted,- disabled = false,- selected = false,- children,- suffix,- ...props- },- ref- ) => {- return (- <ListboxItemBase- {...props}- ref={ref}- {...(highlighted ? { ["data-highlighted"]: true } : {})}- {...(disabled ? { ["aria-disabled"]: true, disabled: true } : {})}- tabIndex={disabled ? -1 : 0}- >- {selected && <CheckIcon />}- <Flex css={{ gridColumn: 2 }} align="center">- <Text variant="label" truncate color={disabled ? "hint" : "contrast"}>- {children}- </Text>- {suffix}- </Flex>- </ListboxItemBase>- );- }-);-ListboxItem.displayName = "ListboxItem";diff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsxindex 697c8c4743..539475f1d7 100644--- a/packages/design-system/src/components/combobox.tsx+++ b/packages/design-system/src/components/combobox.tsx@@ -51,7 +51,7 @@ const ListboxItem = styled("li", itemCss, {margin: 0,});-export const ListboxItemBase: ForwardRefRenderFunction<+const ListboxItemBase: ForwardRefRenderFunction<HTMLLIElement,ComponentProps<typeof ListboxItem> & {disabled?: boolean;diff --git a/packages/design-system/src/components/list.stories.tsx b/packages/design-system/src/components/list.stories.tsxindex 1b3184f931..0149088511 100644--- a/packages/design-system/src/components/list.stories.tsx+++ b/packages/design-system/src/components/list.stories.tsx@@ -18,6 +18,7 @@ export const Simple: ComponentStory<typeof List> = () => {<ListItemprefix={<CheckIcon />}suffix={<DotsHorizontalIcon />}+ currentstate="selected">Orangediff --git a/packages/design-system/src/components/list.tsx b/packages/design-system/src/components/list.tsxindex 4bf3c75e14..a415ca55a7 100644--- a/packages/design-system/src/components/list.tsx+++ b/packages/design-system/src/components/list.tsx@@ -3,7 +3,7 @@ import { styled } from "../stitches.config";import { Flex } from "./flex";import { Text } from "./text";-export const List = styled("ul", {+const ListBase = styled("ul", {display: "flex",flexDirection: "column",margin: 0,@@ -33,20 +33,32 @@ const ListItemBase = styled("li", {},});+export const List = forwardRef<+ HTMLUListElement,+ ComponentProps<typeof ListBase>+>((props, ref) => {+ return <ListBase role="listbox" ref={ref} {...props} />;+});+List.displayName = "List";+export const ListItem = forwardRef<HTMLLIElement,- Omit<ComponentProps<typeof ListItemBase>, "prefix" | "suffix"> & {+ Omit<ComponentProps<typeof ListItemBase>, "prefix" | "suffix" | "current"> & {state?: "disabled" | "selected";prefix?: JSX.Element;suffix?: JSX.Element;+ current?: boolean;}->(({ children, prefix, suffix, state, ...props }, ref) => {+>(({ children, prefix, suffix, state, current, ...props }, ref) => {return (<ListItemBase- {...props}ref={ref}state={state}tabIndex={state === "disabled" ? -1 : 0}+ role="option"+ {...(state === "selected" ? { "aria-selected": true } : undefined)}+ {...(current ? { "aria-current": true } : undefined)}+ {...props}>{prefix}<Flex css={{ gridColumn: 2 }} align="center">diff --git a/packages/design-system/src/index.ts b/packages/design-system/src/index.tsindex 4cf5adcdbd..4f5d0e30ae 100644--- a/packages/design-system/src/index.ts+++ b/packages/design-system/src/index.ts@@ -52,3 +52,4 @@ export * from "./components/tree";export * from "./components/__DEPRECATED__/text";export * from "./components/text";export * from "./components/dialog";+export * from "./components/list";