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
rewrite icon/icon button handling inside text-field
Oleg Isonenad4e57a83e28
diff --git a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsxindex cca153a367..a4026cb7c2 100644--- a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx+++ b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx@@ -32,7 +32,7 @@ export const FontFamilyControl = ({{/* @todo this should be part of the design system, probably a varian="button" */}<TextFielddefaultValue={value.value}- variant={isOpen ? "active" : undefined}+ state={isOpen ? "active" : undefined}/></ValuePickerPopover>);diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsxindex 504bfcd149..225ce4f213 100644--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx@@ -7,8 +7,8 @@ import {ComboboxPopperAnchor,ComboboxListbox,ComboboxListboxItem,- IconButton,numericScrubControl,+ TextFieldIconButton,} from "@webstudio-is/design-system";import { ChevronDownIcon } from "@webstudio-is/icons";import {@@ -263,9 +263,12 @@ export const CssValueInput = ({const suffix =value.type === "keyword" ? (- <IconButton {...getToggleButtonProps()}>+ <TextFieldIconButton+ {...getToggleButtonProps()}+ state={isOpen ? "active" : undefined}+ ><ChevronDownIcon />- </IconButton>+ </TextFieldIconButton>) : value.type === "unit" ? (unitSelectElement) : null;diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsxindex dc2f8aad55..d99374e33e 100644--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx@@ -8,6 +8,7 @@ import {SelectViewport,SelectItem,SelectContent,+ TextFieldIconButton,} from "@webstudio-is/design-system";import { ChevronDownIcon, ChevronUpIcon } from "@webstudio-is/icons";@@ -75,21 +76,8 @@ export const useUnitSelect = ({return [isOpen, select];};-const StyledTrigger = styled(SelectPrimitive.SelectTrigger, {- all: "unset",- height: "$5",- px: "$1",- borderRadius: 2,- display: "inline-flex",- alignItems: "center",- color: "$hiContrast",- "&:hover": {- backgroundColor: "$slate6",- },- "&:focus": {- backgroundColor: "$blue10",- color: "$loContrast",- },+const StyledTrigger = styled(TextFieldIconButton, {+ px: 3,});type UnitSelectProps = {@@ -116,9 +104,11 @@ const UnitSelect = ({onOpenChange={onOpenChange}open={open}>- <StyledTrigger>- <SelectPrimitive.Value>{value}</SelectPrimitive.Value>- </StyledTrigger>+ <SelectPrimitive.SelectTrigger asChild>+ <StyledTrigger>+ <SelectPrimitive.Value>{value}</SelectPrimitive.Value>+ </StyledTrigger>+ </SelectPrimitive.SelectTrigger><SelectPrimitive.Portal><SelectContent onCloseAutoFocus={onCloseAutoFocus}><SelectScrollUpButton>diff --git a/packages/design-system/src/components/icon-button.tsx b/packages/design-system/src/components/icon-button.tsxindex 86080c893a..347f1316df 100644--- a/packages/design-system/src/components/icon-button.tsx+++ b/packages/design-system/src/components/icon-button.tsx@@ -65,12 +65,6 @@ export const IconButton = styled("button", {width: "$8",},},- state: {- active: {- color: "$loContrast",- backgroundColor: "$blue10",- },- },},defaultVariants: {size: "1",diff --git a/packages/design-system/src/components/text-field.stories.tsx b/packages/design-system/src/components/text-field.stories.tsxindex e6c54b8a4f..0a7fa39955 100644--- a/packages/design-system/src/components/text-field.stories.tsx+++ b/packages/design-system/src/components/text-field.stories.tsx@@ -42,8 +42,8 @@ export const Variants: ComponentStory<typeof TextField> = () => {<Flex direction="column" gap={3}><TextField /><TextField variant="ghost" />- <TextField variant="active" />- <TextField type="button" variant="active" />+ <TextField type="button" />+ <TextField type="button" state="active" /></Flex>);};diff --git a/packages/design-system/src/components/text-field.tsx b/packages/design-system/src/components/text-field.tsxindex 8c895e80b8..391ad02f2a 100644--- a/packages/design-system/src/components/text-field.tsx+++ b/packages/design-system/src/components/text-field.tsx@@ -1,7 +1,7 @@import { mergeRefs } from "@react-aria/utils";import React from "react";import { useFocusWithin } from "@react-aria/interactions";-import { styled } from "../stitches.config";+import { css, styled } from "../stitches.config";import { Flex } from "./flex";import { ChevronLeftIcon } from "@webstudio-is/icons";import { cssVars } from "@webstudio-is/css-vars";@@ -9,7 +9,7 @@ import { cssVars } from "@webstudio-is/css-vars";const backgroundColorVar = cssVars.define("background-color");const colorVar = cssVars.define("color");-const getButtonSuffixCssVars = (state: "focus" | "hover") => {+const getTextFieldSuffixCssVars = (state: "focus" | "hover") => {if (state === "focus") {return {[backgroundColorVar]: "$colors$blue10",@@ -23,9 +23,47 @@ const getButtonSuffixCssVars = (state: "focus" | "hover") => {};};-const ButtonSuffix = styled(ChevronLeftIcon, {- borderRadius: 2, // @todo shold come from theme+const textFieldIconBaseStyle = css({height: "$5",+ minWidth: "$2",+ display: "inline-flex",+ alignItems: "center",+ justifyContent: "center",+ borderRadius: 2,+});++// Trigger can be used as a button, which is focusable/hoverable itself or as an icon,+// which has the same states but activated over the parent component element.+export const TextFieldIconButton = styled(+ "button",+ {+ all: "unset",+ "&:hover": {+ backgroundColor: "$slate7",+ color: "$hiContrast",+ },+ "&:focus": {+ backgroundColor: "$blue10",+ color: "white",+ },+ variants: {+ state: {+ active: {+ backgroundColor: "$blue10",+ color: "white",+ "&:hover": {+ backgroundColor: "$blue10",+ color: "white",+ },+ },+ },+ },+ },+ textFieldIconBaseStyle+);++export const TextFieldIcon = styled("span", textFieldIconBaseStyle, {+ // Icon receives colors from parent.backgroundColor: cssVars.use(backgroundColorVar),color: cssVars.use(colorVar),});@@ -104,26 +142,20 @@ const TextFieldBase = styled("div", {fontSize: "$1",height: 28, // @todo waiting for the sizing scalelineHeight: 1,-- "&:hover": getButtonSuffixCssVars("hover"),-"&:focus-within": {boxShadow:"inset 0px 0px 0px 1px $colors$blue10, 0px 0px 0px 1px $colors$blue10",- ...getButtonSuffixCssVars("focus"),},-"&[aria-disabled=true]": {pointerEvents: "none",backgroundColor: "$slate2",},- "&:is(input:read-only)": {+ "&:has(input:read-only)": {backgroundColor: "$slate2","&:focus": {boxShadow: "inset 0px 0px 0px 1px $colors$slate7",},},-variants: {variant: {ghost: {@@ -137,7 +169,7 @@ const TextFieldBase = styled("div", {"&:focus": {backgroundColor: "$loContrast",boxShadow:- "inset 0px 0px 0px 1px $colors$blue8, 0px 0px 0px 1px $colors$blue10",+ "inset 0px 0px 0px 1px $colors$blue10, 0px 0px 0px 1px $colors$blue10",},"&:disabled": {backgroundColor: "transparent",@@ -146,10 +178,9 @@ const TextFieldBase = styled("div", {backgroundColor: "transparent",},},- active: {- boxShadow:- "inset 0px 0px 0px 1px $colors$blue10, 0px 0px 0px 1px $colors$blue10",- ...getButtonSuffixCssVars("focus"),+ button: {+ "&:hover": getTextFieldSuffixCssVars("hover"),+ "&:focus-within": getTextFieldSuffixCssVars("focus"),},},state: {@@ -167,20 +198,44 @@ const TextFieldBase = styled("div", {"inset 0px 0px 0px 1px $colors$green8, 0px 0px 0px 1px $colors$green8",},},+ active: {+ boxShadow:+ "inset 0px 0px 0px 1px $colors$blue10, 0px 0px 0px 1px $colors$blue10",+ ...getTextFieldSuffixCssVars("focus"),+ },},+ // Preffix and suffix are responsible for their spacingwithPrefix: {true: {- paddingLeft: 2,+ paddingLeft: 0,},},withSuffix: {true: {- paddingRight: 2,+ paddingRight: 0,},},},});+const PrefixSlot = styled(Flex, {+ alignItems: "center",+ flexShrink: 0,+ order: 0,+ padding: 2,+ paddingRight: 0,+ borderRadius: 2,+});++const SuffixSlot = styled(Flex, {+ alignItems: "center",+ flexShrink: 0,+ order: 2,+ padding: 2,+ paddingLeft: 0,+ borderRadius: 2,+});+export type TextFieldProps = Pick<React.ComponentProps<typeof TextFieldBase>,"variant" | "state" | "css"@@ -199,16 +254,18 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(disabled,inputRef,state,- variant,+ variant: variantProp,onFocus,onBlur,onClick,type,// prevent spreading it into the dom- suffix: _suffix,+ suffix: suffixProp,...textFieldProps} = props;- let { suffix } = props;+ let suffix = suffixProp;+ const variant =+ type === "button" && variantProp === undefined ? "button" : variantProp;const internalInputRef = React.useRef<HTMLInputElement>(null);@@ -226,7 +283,8 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(if (type === "button" && suffix === undefined) {suffix = (- <ButtonSuffix+ <TextFieldIcon+ as={ChevronLeftIcon}onClick={() => {internalInputRef.current?.click();}}@@ -255,29 +313,8 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(ref={mergeRefs(internalInputRef, inputRef ?? null)}/>- {prefix && (- <Flex- css={{- alignItems: "center",- flexShrink: 0,- order: 0,- }}- >- {prefix}- </Flex>- )}-- {suffix && (- <Flex- css={{- alignItems: "center",- flexShrink: 0,- order: 2,- }}- >- {suffix}- </Flex>- )}+ {prefix && <PrefixSlot>{prefix}</PrefixSlot>}+ {suffix && <SuffixSlot>{suffix}</SuffixSlot>}</TextFieldBase>);}diff --git a/packages/design-system/src/index.ts b/packages/design-system/src/index.tsindex 141942cd68..4cf5adcdbd 100644--- a/packages/design-system/src/index.ts+++ b/packages/design-system/src/index.ts@@ -10,7 +10,7 @@ export { Label } from "./components/label";export * as ToggleGroup from "./components/toggle-group";export * from "./components/progress-radial";export * from "./components/select";-export { TextField } from "./components/text-field";+export * from "./components/text-field";export { SearchField } from "./components/search-field";export { Flex } from "./components/flex";export { Image } from "./components/image";