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
feat(Select): Implement Select using @radix-ui/select
Andrey Okonetchnikov22bcd9e96d38
diff --git a/app/shared/design-system/components/select.stories.tsx b/app/shared/design-system/components/select.stories.tsxindex 387a375f9d..030ff9232c 100644--- a/app/shared/design-system/components/select.stories.tsx+++ b/app/shared/design-system/components/select.stories.tsx@@ -1,22 +1,70 @@-import React from "react";import { ComponentStory } from "@storybook/react";-import { Select } from "~/shared/design-system";+import React from "react";+import { Select, SelectOption } from "~/shared/design-system";export default {component: Select,};-const values = [- { value: "Apple", label: "Apple" },- { value: "Banana", label: "Banana" },- { value: "Orange", label: "Orange" },- { value: "Pear", label: "Pear" },- { value: "Grape", label: "Grape" },-];+export const Simple: ComponentStory<typeof Select> = () => {+ const options = ["Apple", "Banana", "Orange"];+ const [value, setValue] = React.useState(options[0]);+ return (+ <Select name="fruit" options={options} value={value} onChange={setValue} />+ );+};++export const Ghost: ComponentStory<typeof Select> = () => {+ return (+ <Select+ name="fruit"+ options={["Apple", "Banana", "Orange"]}+ defaultValue="Apple"+ ghost+ />+ );+};++export const FullWidth: ComponentStory<typeof Select> = () => {+ return (+ <Select+ name="fruit"+ options={["Apple", "Banana", "Orange"]}+ defaultValue="Apple"+ fullWidth+ />+ );+};++export const WithComplexItems: ComponentStory<typeof Select> = () => {+ const items = {+ apple: { icon: "🍎" },+ banana: { icon: "🍌" },+ orange: { icon: "🍊" },+ pear: { icon: "🍐" },+ grape: { icon: "🍇" },+ } as const;+ const options = Object.keys(items);+ const [value, setValue] = React.useState(options[0]);+ const getLabel = (option: SelectOption) =>+ value ? `${items[option]?.icon} ${option}` : "No fruit selected";+ return (+ <Select+ name="fruit"+ options={options}+ value={value}+ onChange={setValue}+ getLabel={getLabel}+ />+ );+};-export const Default: ComponentStory<typeof Select> = () => {- const [value, setValue] = React.useState();+export const Boundaries: ComponentStory<typeof Select> = () => {+ const items = Array(100)+ .fill(0)+ .map((_, i) => `Item ${i}`);+ const [value, setValue] = React.useState(items[0]);return (- <Select name="fruit" options={values} value={value} onChange={setValue} />+ <Select name="fruit" options={items} value={value} onChange={setValue} />);};diff --git a/app/shared/design-system/components/select.tsx b/app/shared/design-system/components/select.tsxindex 814c1be9c3..345cad19eb 100644--- a/app/shared/design-system/components/select.tsx+++ b/app/shared/design-system/components/select.tsx@@ -1,25 +1,33 @@-import React, { Ref } from "react";-import { CSS, styled } from "../stitches.config";-import { CaretSortIcon } from "~/shared/icons";+import * as SelectPrimitive from "@radix-ui/react-select";+import noop from "lodash.noop";+import React, { ReactNode, Ref } from "react";+import {+ CaretSortIcon,+ CheckIcon,+ ChevronDownIcon,+ ChevronUpIcon,+} from "~/shared/icons";+import { styled } from "../stitches.config";-const SelectWrapper = styled("div", {- backgroundColor: "$loContrast",- borderRadius: "$1",- boxShadow: "inset 0 0 0 1px $colors$slate7",- color: "$hiContrast",- fontFamily: "$untitled",+const StyledTrigger = styled(SelectPrimitive.SelectTrigger, {+ all: "unset",+ display: "inline-flex",+ alignItems: "center",+ justifyContent: "space-between",+ padding: "0 $1 0 $2",fontSize: "$1",fontVariantNumeric: "tabular-nums",- fontWeight: 400,height: "$5",+ gap: "$2",flexShrink: 0,-+ borderRadius: "$1",+ backgroundColor: "$loContrast",+ color: "$hiContrast",+ boxShadow: "inset 0 0 0 1px $colors$slate7","&:hover": {backgroundColor: "$slateA3",},-- "&:focus-within": {- zIndex: 1,+ "&:focus": {boxShadow:"inset 0px 0px 0px 1px $colors$blue8, 0px 0px 0px 1px $colors$blue8",},@@ -31,63 +39,161 @@ const SelectWrapper = styled("div", {boxShadow: "none",},},+ fullWidth: {+ true: {+ width: "100%",+ },+ },},});-const StyledSelect = styled("select", {- appearance: "none",- backgroundColor: "transparent",- border: "none",- borderRadius: "inherit",- color: "inherit",- font: "inherit",- outline: "none",- width: "100%",- height: "100%",- pl: "$1",- pr: "$3",+const StyledIcon = styled(SelectPrimitive.Icon, {+ display: "inline-flex",+ alignItems: "center",});-const StyledCaretSortIcon = styled(CaretSortIcon, {- position: "absolute",- pointerEvents: "none",- display: "inline",+const StyledContent = styled(SelectPrimitive.Content, {+ overflow: "hidden",+ backgroundColor: "white",+ borderRadius: 6,+ boxShadow:+ "0px 10px 38px -10px rgba(22, 23, 24, 0.35), 0px 10px 20px -15px rgba(22, 23, 24, 0.2)",+});- // Use margins instead of top/left to avoid setting "position: relative" on parent,- // which would make stacking context tricky with Select used in a control group.- marginTop: 5,- marginLeft: -16,+const StyledViewport = styled(SelectPrimitive.Viewport, {+ py: "$1",});-export type Option = { value: string; label: string };+const StyledItem = styled(SelectPrimitive.Item, {+ all: "unset",+ fontSize: "$1",+ lineHeight: 1,+ color: "$hiContrast",+ borderRadius: "$1",+ display: "flex",+ alignItems: "center",+ height: "$5",+ padding: "0 35px 0 25px",+ position: "relative",+ userSelect: "none",++ "&[data-disabled]": {+ color: "$loContrast",+ pointerEvents: "none",+ },-export type SelectProps<T = Option> = React.ComponentProps<- typeof StyledSelect+ "&:focus": {+ backgroundColor: "$slateA3",+ color: "$hiContrast",+ },+});++const StyledItemIndicator = styled(SelectPrimitive.ItemIndicator, {+ position: "absolute",+ left: 0,+ width: 25,+ display: "inline-flex",+ alignItems: "center",+ justifyContent: "center",+});++const scrollButtonStyles = {+ display: "flex",+ alignItems: "center",+ justifyContent: "center",+ height: 25,+ backgroundColor: "white",+ color: "$hiContrast",+ cursor: "default",+};++const SelectScrollUpButton = styled(+ SelectPrimitive.ScrollUpButton,+ scrollButtonStyles+);++const SelectScrollDownButton = styled(+ SelectPrimitive.ScrollDownButton,+ scrollButtonStyles+);++const SelectItemBase = ({ children, ...props }: SelectItemProps) => {+ return (+ <StyledItem {...props}>+ <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>+ <StyledItemIndicator>+ <CheckIcon />+ </StyledItemIndicator>+ </StyledItem>+ );+};++type SelectItemProps = SelectPrimitive.SelectItemProps & {+ children: ReactNode;+};+const SelectItem = React.forwardRef(SelectItemBase);++export type SelectOption = string;++export type SelectProps<T = SelectOption> = Omit<+ React.ComponentProps<typeof StyledTrigger>,+ "onChange" | "value" | "defaultValue"> & {options: T[];+ defaultValue?: T;value?: T;- onChange?: (value: T) => void;- css?: CSS;+ onChange?: (option: T) => void;+ placeholder?: string;+ getLabel?: (option: T) => string;};const SelectBase = (- { css, options, ...props }: SelectProps,- forwardedRef: Ref<HTMLSelectElement>+ {+ options,+ value,+ defaultValue,+ placeholder = "Select an option",+ onChange = noop,+ getLabel = (option) => option,+ name,+ ...props+ }: SelectProps,+ forwardedRef: Ref<HTMLButtonElement>) => {return (- <SelectWrapper css={css}>- <StyledSelect ref={forwardedRef} {...props}>- {options.map((option) => (- <option key={option.value} value={option.value}>- {option.label}- </option>- ))}- </StyledSelect>- <StyledCaretSortIcon />- </SelectWrapper>+ <SelectPrimitive.Root+ name={name}+ value={value}+ defaultValue={defaultValue}+ onValueChange={onChange}+ >+ <StyledTrigger ref={forwardedRef} {...props}>+ <SelectPrimitive.Value>+ {value ? getLabel(value) : placeholder}+ </SelectPrimitive.Value>+ <StyledIcon>+ <CaretSortIcon />+ </StyledIcon>+ </StyledTrigger>++ <StyledContent>+ <SelectScrollUpButton>+ <ChevronUpIcon />+ </SelectScrollUpButton>+ <StyledViewport>+ {options.map((option) => (+ <SelectItem key={option} value={option} textValue={option}>+ {getLabel(option)}+ </SelectItem>+ ))}+ </StyledViewport>+ <SelectScrollDownButton>+ <ChevronDownIcon />+ </SelectScrollDownButton>+ </StyledContent>+ </SelectPrimitive.Root>);};export const Select = React.forwardRef(SelectBase);Select.displayName = "Select";-Select.toString = () => `.${SelectWrapper.className}`;diff --git a/app/shared/design-system/index.ts b/app/shared/design-system/index.tsindex 5112a0666e..ce9846ff11 100644--- a/app/shared/design-system/index.ts+++ b/app/shared/design-system/index.ts@@ -19,7 +19,7 @@ export * as Portal from "@radix-ui/react-portal";export { Container } from "./components/container";export { Label } from "./components/label";export * as ToggleGroup from "./components/toggle-group";-export { Select } from "./components/select";+export * from "./components/select";export { TextField } from "./components/text-field";export { Flex } from "./components/flex";export { Grid } from "./components/grid";diff --git a/package.json b/package.jsonindex 914ebb5b5d..ebba48d962 100644--- a/package.json+++ b/package.json@@ -40,6 +40,7 @@"@radix-ui/react-portal": "^0.1.4","@radix-ui/react-progress": "^0.1.4","@radix-ui/react-radio-group": "^0.1.5",+ "@radix-ui/react-select": "^0.1.1","@radix-ui/react-separator": "^0.1.4","@radix-ui/react-slider": "^0.1.4","@radix-ui/react-switch": "^0.1.5",diff --git a/yarn.lock b/yarn.lockindex 86e1f730ad..175b15af35 100644--- a/yarn.lock+++ b/yarn.lock@@ -2371,6 +2371,31 @@"@radix-ui/react-use-callback-ref" "0.1.0""@radix-ui/react-use-controllable-state" "0.1.0"+"@radix-ui/react-select@^0.1.1":+ version "0.1.1"+ resolved "https://registry.yarnpkg.com/@radix-ui/react-select/-/react-select-0.1.1.tgz#ceedea6856a37e4079492e1c69601797cedd2c85"+ integrity sha512-xY3jLz2c6ZBHflldGsA79bE/swUfRMpiRPQf+JDihOufsd3z5uW22PFe0MS5bGZiIpK6aAZAvwqEd2Bu7hqp8w==+ dependencies:+ "@babel/runtime" "^7.13.10"+ "@radix-ui/number" "0.1.0"+ "@radix-ui/primitive" "0.1.0"+ "@radix-ui/react-collection" "0.1.4"+ "@radix-ui/react-compose-refs" "0.1.0"+ "@radix-ui/react-context" "0.1.1"+ "@radix-ui/react-dismissable-layer" "0.1.5"+ "@radix-ui/react-focus-scope" "0.1.4"+ "@radix-ui/react-id" "0.1.5"+ "@radix-ui/react-label" "0.1.5"+ "@radix-ui/react-portal" "0.1.4"+ "@radix-ui/react-primitive" "0.1.4"+ "@radix-ui/react-use-callback-ref" "0.1.0"+ "@radix-ui/react-use-controllable-state" "0.1.0"+ "@radix-ui/react-use-layout-effect" "0.1.0"+ "@radix-ui/react-use-previous" "0.1.1"+ "@radix-ui/react-visually-hidden" "0.1.4"+ aria-hidden "^1.1.1"+ react-remove-scroll "^2.4.0"+"@radix-ui/react-separator@^0.1.4":version "0.1.4"resolved "https://registry.yarnpkg.com/@radix-ui/react-separator/-/react-separator-0.1.4.tgz#383ad0f82b364d9982a978d752084af3598e4090"