Skip to content

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.

Style Source Input (#692)

Oleg Isonen2f0ac598950c
diff --git a/.lostpixel/baseline/components-button--simple.png b/.lostpixel/baseline/components-button--simple.png
new file mode 100644
index 0000000000..df720dbeb1
Binary files /dev/null and b/.lostpixel/baseline/components-button--simple.png differ
diff --git a/.lostpixel/baseline/components-combobox--simple.png b/.lostpixel/baseline/components-combobox--simple.png
index c5bdaf570a..2b015ec567 100644
Binary files a/.lostpixel/baseline/components-combobox--simple.png and b/.lostpixel/baseline/components-combobox--simple.png differ
diff --git a/.lostpixel/baseline/components-enhanced-tooltip--simple.png b/.lostpixel/baseline/components-enhanced-tooltip--simple.png
index 6400b19900..612a6fee60 100644
Binary files a/.lostpixel/baseline/components-enhanced-tooltip--simple.png and b/.lostpixel/baseline/components-enhanced-tooltip--simple.png differ
diff --git a/.lostpixel/baseline/components-text--truncated.png b/.lostpixel/baseline/components-text--truncated.png
new file mode 100644
index 0000000000..30eeae71ff
Binary files /dev/null and b/.lostpixel/baseline/components-text--truncated.png differ
diff --git a/.lostpixel/baseline/components-text-field--focus-events.png b/.lostpixel/baseline/components-text-field--focus-events.png
index 5542e8888a..f8d9877b37 100644
Binary files a/.lostpixel/baseline/components-text-field--focus-events.png and b/.lostpixel/baseline/components-text-field--focus-events.png differ
diff --git a/.lostpixel/baseline/components-text-field--layout.png b/.lostpixel/baseline/components-text-field--layout.png
index 6f721d6ca4..25ca31269c 100644
Binary files a/.lostpixel/baseline/components-text-field--layout.png and b/.lostpixel/baseline/components-text-field--layout.png differ
diff --git a/.lostpixel/baseline/components-text-field--native-props.png b/.lostpixel/baseline/components-text-field--native-props.png
index d0e9a63194..4c2d5f9482 100644
Binary files a/.lostpixel/baseline/components-text-field--native-props.png and b/.lostpixel/baseline/components-text-field--native-props.png differ
diff --git a/apps/designer/app/designer/features/style-panel/style-source-input/index.ts b/apps/designer/app/designer/features/style-panel/style-source-input/index.ts
new file mode 100644
index 0000000000..a4729cd9f9
--- /dev/null
+++ b/apps/designer/app/designer/features/style-panel/style-source-input/index.ts
@@ -0,0 +1 @@
+export * from "./style-source-input";
diff --git a/apps/designer/app/designer/features/style-panel/style-source-input/style-source-input.stories.tsx b/apps/designer/app/designer/features/style-panel/style-source-input/style-source-input.stories.tsx
new file mode 100644
index 0000000000..8b84e62818
--- /dev/null
+++ b/apps/designer/app/designer/features/style-panel/style-source-input/style-source-input.stories.tsx
@@ -0,0 +1,100 @@
+import { v4 as uuid } from "uuid";
+import type { ComponentStory } from "@storybook/react";
+import { useState } from "react";
+import { StyleSourceInput } from "./style-source-input";
+
+export default {
+ component: StyleSourceInput,
+};
+
+const items = [
+ { id: "1", label: "Apple", type: "token" },
+ { id: "2", label: "Banana", type: "token" },
+ { id: "3", label: "Orange", type: "token" },
+];
+
+export const Initial: ComponentStory<typeof StyleSourceInput> = () => {
+ const [value, setValue] = useState([
+ { id: "0", label: "Local", type: "local" },
+ ]);
+ return (
+ <StyleSourceInput
+ css={{ width: 300 }}
+ items={items}
+ value={value}
+ onCreate={({ label }) => {
+ const item = { id: uuid(), label, type: "token" };
+ setValue([...value, item]);
+ }}
+ onSelect={(item) => {
+ setValue([...value, item]);
+ }}
+ onRemove={(itemToRemove) => {
+ if (itemToRemove.type === "local") {
+ return;
+ }
+ setValue(value.filter((item) => item.id !== itemToRemove.id));
+ }}
+ />
+ );
+};
+
+export const WithItems: ComponentStory<typeof StyleSourceInput> = () => {
+ const [value, setValue] = useState([
+ { id: "0", label: "Local", type: "local" },
+ ...items,
+ ]);
+ return (
+ <StyleSourceInput
+ css={{ width: 300 }}
+ items={items}
+ value={value}
+ onCreate={({ label }) => {
+ const item = { id: uuid(), label, type: "token" };
+ setValue([...value, item]);
+ }}
+ onSelect={(item) => {
+ setValue([...value, item]);
+ }}
+ onRemove={(itemToRemove) => {
+ if (itemToRemove.type === "local") {
+ return;
+ }
+ setValue(value.filter((item) => item.id !== itemToRemove.id));
+ }}
+ />
+ );
+};
+
+export const WithTruncatedItem: ComponentStory<
+ typeof StyleSourceInput
+> = () => {
+ const [value, setValue] = useState([
+ {
+ id: "0",
+ label:
+ "Local Something Something Something Something Something Something Something Something Something Something Something",
+ type: "local",
+ },
+ ]);
+ return (
+ <StyleSourceInput
+ css={{ width: 300 }}
+ items={items}
+ value={value}
+ onCreate={({ label }) => {
+ const item = { id: uuid(), label, type: "token" };
+ setValue([...value, item]);
+ }}
+ onSelect={(item) => {
+ setValue([...value, item]);
+ }}
+ onRemove={(itemToRemove) => {
+ if (itemToRemove.type === "local") {
+ return;
+ }
+ setValue(value.filter((item) => item.id !== itemToRemove.id));
+ }}
+ />
+ );
+};
diff --git a/apps/designer/app/designer/features/style-panel/style-source-input/style-source-input.tsx b/apps/designer/app/designer/features/style-panel/style-source-input/style-source-input.tsx
new file mode 100644
index 0000000000..2b41c3c1d6
--- /dev/null
+++ b/apps/designer/app/designer/features/style-panel/style-source-input/style-source-input.tsx
@@ -0,0 +1,183 @@
+import {
+ Box,
+ Button,
+ Text,
+ ComboboxListbox,
+ ComboboxListboxItem,
+ ComboboxPopper,
+ ComboboxPopperAnchor,
+ ComboboxPopperContent,
+ TextFieldContainer,
+ TextFieldInput,
+ useTextFieldFocus,
+ useCombobox,
+ type CSS,
+} from "@webstudio-is/design-system";
+import {
+ ComponentProps,
+ ElementRef,
+ forwardRef,
+ RefObject,
+ useState,
+} from "react";
+import { mergeRefs } from "@react-aria/utils";
+
+type IntermediateItem = {
+ label: string;
+};
+
+type StyleSourceButtonProps = {
+ label: string;
+};
+
+const StyleSourceButton = ({ label }: StyleSourceButtonProps) => {
+ return (
+ <Button variant="gray" css={{ maxWidth: "100%" }}>
+ <Text truncate>{label}</Text>
+ </Button>
+ );
+};
+
+type TextFieldWrapperProps = Omit<ComponentProps<"input">, "value"> &
+ Pick<
+ ComponentProps<typeof TextFieldContainer>,
+ "variant" | "state" | "css"
+ > & {
+ value: Array<IntermediateItem>;
+ label: string;
+ disabled?: boolean;
+ containerRef?: RefObject<HTMLDivElement>;
+ inputRef?: RefObject<HTMLInputElement>;
+ };
+
+const TextField = forwardRef<ElementRef<typeof Box>, TextFieldWrapperProps>(
+ (props, forwardedRef) => {
+ const {
+ css,
+ disabled,
+ containerRef,
+ inputRef,
+ state,
+ variant,
+ onFocus,
+ onBlur,
+ onClick,
+ type,
+ onKeyDown,
+ label,
+ value,
+ ...textFieldProps
+ } = props;
+ const [internalInputRef, focusProps] = useTextFieldFocus({
+ disabled,
+ onFocus,
+ onBlur,
+ });
+
+ return (
+ <TextFieldContainer
+ {...focusProps}
+ aria-disabled={disabled}
+ ref={mergeRefs(forwardedRef, containerRef ?? null)}
+ state={state}
+ variant={variant}
+ css={{ ...css, px: "$spacing$3", py: "$spacing$2" }}
+ onKeyDown={onKeyDown}
+ >
+ {value.map((item, index) => (
+ <StyleSourceButton label={item.label} key={index} />
+ ))}
+ {/* We want input to be the first element in DOM so it receives the focus first */}
+ <TextFieldInput
+ {...textFieldProps}
+ value={label}
+ type={type}
+ disabled={disabled}
+ onClick={onClick}
+ ref={mergeRefs(internalInputRef, inputRef ?? null)}
+ />
+ </TextFieldContainer>
+ );
+ }
+);
+TextField.displayName = "TextField";
+
+type StyleSourceInputProps<Item> = {
+ items?: Array<Item>;
+ value?: Array<Item>;
+ onSelect: (item: Item) => void;
+ onRemove: (item: Item) => void;
+ onCreate: (item: IntermediateItem) => void;
+ css?: CSS;
+};
+
+export const StyleSourceInput = <Item extends IntermediateItem>(
+ props: StyleSourceInputProps<Item>
+) => {
+ const value = props.value ?? [];
+ const [label, setLabel] = useState("");
+ const {
+ items,
+ getInputProps,
+ getComboboxProps,
+ getMenuProps,
+ getItemProps,
+ isOpen,
+ } = useCombobox({
+ items: props.items ?? [],
+ value: { label },
+ selectedItem: undefined,
+ itemToString: (item) => (item ? item.label : ""),
+ onItemSelect(item) {
+ setLabel("");
+ props.onSelect(item as Item);
+ },
+ onInputChange(label) {
+ setLabel(label ?? "");
+ },
+ });
+ const inputProps = getInputProps({
+ onKeyDown(event) {
+ if (event.key === "Backspace" && label === "") {
+ props.onRemove(value[value.length - 1]);
+ return;
+ }
+ },
+ onKeyPress(event) {
+ if (event.key === "Enter" && label.trim() !== "") {
+ setLabel("");
+ props.onCreate({ label });
+ }
+ },
+ });
+
+ return (
+ <ComboboxPopper>
+ <Box {...getComboboxProps()}>
+ <ComboboxPopperAnchor>
+ <TextField
+ {...inputProps}
+ label={label}
+ value={value}
+ css={props.css}
+ />
+ </ComboboxPopperAnchor>
+ <ComboboxPopperContent align="start" sideOffset={5}>
+ <ComboboxListbox {...getMenuProps()}>
+ {isOpen &&
+ items.map((item, index) => {
+ return (
+ <ComboboxListboxItem
+ {...getItemProps({ item, index })}
+ key={index}
+ >
+ {item.label}
+ </ComboboxListboxItem>
+ );
+ })}
+ </ComboboxListbox>
+ </ComboboxPopperContent>
+ </Box>
+ </ComboboxPopper>
+ );
+};
diff --git a/packages/design-system/src/components/button.stories.tsx b/packages/design-system/src/components/button.stories.tsx
new file mode 100644
index 0000000000..9ebf2caf27
--- /dev/null
+++ b/packages/design-system/src/components/button.stories.tsx
@@ -0,0 +1,10 @@
+import type { ComponentStory } from "@storybook/react";
+import { Button } from "./button";
+
+export default {
+ component: Button,
+};
+
+export const Simple: ComponentStory<typeof Button> = () => {
+ return <Button>Simple</Button>;
+};
diff --git a/packages/design-system/src/components/button.tsx b/packages/design-system/src/components/button.tsx
index 31154684c6..18a031d393 100644
--- a/packages/design-system/src/components/button.tsx
+++ b/packages/design-system/src/components/button.tsx
@@ -21,6 +21,7 @@ export const Button = styled("button", {
justifyContent: "center",
lineHeight: 1,
WebkitTapHighlightColor: "rgba(0,0,0,0)",
+ minWidth: 0,
// Custom
height: "$spacing$11",
@@ -42,12 +43,12 @@ export const Button = styled("button", {
"1": {
borderRadius: "$borderRadius$4",
height: "$spacing$11",
- px: "$spacing$9",
+ px: "$spacing$4",
fontSize: "$fontSize$3",
},
"2": {
borderRadius: "$borderRadius$6",
- height: 28, // @todo waiting for the sizing scale
+ height: "$spacing$12",
px: "$spacing$9",
fontSize: "$fontSize$4",
},
diff --git a/packages/design-system/src/components/text-field.tsx b/packages/design-system/src/components/text-field.tsx
index 0350886335..0a037aca4b 100644
--- a/packages/design-system/src/components/text-field.tsx
+++ b/packages/design-system/src/components/text-field.tsx
@@ -1,5 +1,9 @@
import { mergeRefs } from "@react-aria/utils";
-import React from "react";
+import React, {
+ type ComponentProps,
+ type RefObject,
+ type FocusEventHandler,
+} from "react";
import { useFocusWithin } from "@react-aria/interactions";
import { css, styled } from "../stitches.config";
import { ChevronLeftIcon } from "@webstudio-is/icons";
@@ -74,7 +78,7 @@ export const TextFieldIcon = styled("span", textFieldIconBaseStyle, {
color: cssVars.use(colorVar),
});
-const InputBase = styled("input", {
+export const TextFieldInput = styled("input", {
// Reset
appearance: "none",
borderWidth: "0",
@@ -83,12 +87,12 @@ const InputBase = styled("input", {
fontFamily: "inherit",
fontSize: "inherit",
color: "inherit",
- margin: "0",
padding: "0",
+ height: "$spacing$9",
flexGrow: 1,
flexShrink: 1,
+ flexBasis: "$spacing$10",
minWidth: 0,
- width: "100%",
textOverflow: "ellipsis",
outline: "none",
WebkitTapHighlightColor: "rgba(0,0,0,0)",
@@ -134,19 +138,21 @@ const InputBase = styled("input", {
},
});
-const TextFieldBase = styled("div", {
+export const TextFieldContainer = styled("div", {
// Custom
display: "flex",
+ flexWrap: "wrap",
+ alignItems: "center",
backgroundColor: "$loContrast",
boxShadow: "inset 0 0 0 1px $colors$slate7",
color: "$hiContrast",
fontVariantNumeric: "tabular-nums",
gap: "$spacing$3",
- px: "$spacing$5",
+ px: "$spacing$4",
borderRadius: "$borderRadius$4",
fontFamily: "$sans",
fontSize: "$fontSize$3",
- height: 28, // @todo waiting for the sizing scale
+ minHeight: "$spacing$12",
lineHeight: 1,
minWidth: 0,
"&:focus-within": {
@@ -245,12 +251,52 @@ const SuffixSlot = styled("div", {
borderRadius: 2,
});
+export const useTextFieldFocus = ({
+ disabled,
+ onFocus,
+ onBlur,
+}: {
+ disabled?: boolean;
+ onFocus?: FocusEventHandler<HTMLInputElement>;
+ onBlur?: FocusEventHandler<HTMLInputElement>;
+}): [
+ RefObject<HTMLInputElement>,
+ ComponentProps<typeof TextFieldContainer>
+] => {
+ const ref = React.useRef<HTMLInputElement>(null);
+
+ const onClickCapture = React.useCallback(() => {
+ ref.current?.focus();
+ }, [ref]);
+
+ const { focusWithinProps } = useFocusWithin({
+ isDisabled: disabled,
+ // @ts-expect-error Type mismatch from react-aria
+ onFocusWithin: onFocus,
+ // @ts-expect-error Type mismatch from react-aria
+ onBlurWithin: onBlur,
+ });
+
+ return [
+ ref,
+ {
+ ...focusWithinProps,
+ onClickCapture,
+ // Setting tabIndex to -1 to allow this element to be focused via JavaScript.
+ // This is used when we need to hide the caret but want to:
+ // 1. keep the visual focused state of the component
+ // 2. keep focus somewhere insisde the component to not trigger some focus-trap logic
+ tabIndex: -1,
+ },
+ ];
+};
+
export type TextFieldProps = Pick<
- React.ComponentProps<typeof TextFieldBase>,
+ React.ComponentProps<typeof TextFieldContainer>,
"variant" | "state" | "css"
> &
Omit<React.ComponentProps<"input">, "prefix" | "children"> & {
- baseRef?: React.Ref<HTMLDivElement>;
+ containerRef?: React.Ref<HTMLDivElement>;
inputRef?: React.Ref<HTMLInputElement>;
prefix?: React.ReactNode;
suffix?: React.ReactNode;
@@ -262,7 +308,7 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
prefix,
css,
disabled,
- baseRef,
+ containerRef,
inputRef,
state,
variant: variantProp,
@@ -279,18 +325,10 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
const variant =
type === "button" && variantProp === undefined ? "button" : variantProp;
- const internalInputRef = React.useRef<HTMLInputElement>(null);
-
- const focusInnerInput = React.useCallback(() => {
- internalInputRef.current?.focus();
- }, [internalInputRef]);
-
- const { focusWithinProps } = useFocusWithin({
- isDisabled: disabled,
- // @ts-expect-error Type mismatch from react-aria
- onFocusWithin: onFocus,
- // @ts-expect-error Type mismatch from react-aria
- onBlurWithin: onBlur,
+ const [internalInputRef, focusProps] = useTextFieldFocus({
+ disabled,
+ onFocus,
+ onBlur,
});
if (type === "button" && suffix === undefined) {
@@ -305,25 +343,19 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
}
return (
- <TextFieldBase
- {...focusWithinProps}
+ <TextFieldContainer
+ {...focusProps}
aria-disabled={disabled}
- ref={mergeRefs(forwardedRef, baseRef ?? null)}
+ ref={mergeRefs(forwardedRef, containerRef ?? null)}
state={state}
variant={variant}
css={css}
withPrefix={Boolean(prefix)}
withSuffix={Boolean(suffix)}
- onClickCapture={focusInnerInput}
onKeyDown={onKeyDown}
- // Setting tabIndex to -1 to allow this element to be focused via JavaScript.
- // This is used when we need to hide the caret but want to:
- // 1. keep the visual focused state of the component
- // 2. keep focus somewhere insisde the component to not trigger some focus-trap logic
- tabIndex={-1}
>
{/* We want input to be the first element in DOM so it receives the focus first */}
- <InputBase
+ <TextFieldInput
{...textFieldProps}
type={type}
disabled={disabled}
@@ -333,7 +365,7 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
{prefix && <PrefixSlot>{prefix}</PrefixSlot>}
{suffix && <SuffixSlot>{suffix}</SuffixSlot>}
- </TextFieldBase>
+ </TextFieldContainer>
);
}
);
diff --git a/packages/design-system/src/components/text.stories.tsx b/packages/design-system/src/components/text.stories.tsx
index 8ceb1c6330..71342a6968 100644
--- a/packages/design-system/src/components/text.stories.tsx
+++ b/packages/design-system/src/components/text.stories.tsx
@@ -25,3 +25,10 @@ export const Mono: ComponentStory<typeof Text> = () => {
export const Unit: ComponentStory<typeof Text> = () => {
return <Text variant="unit">Unit text</Text>;
};
+export const Truncated: ComponentStory<typeof Text> = () => {
+ return (
+ <Text truncate css={{ width: 200 }}>
+ Long long text asdf asdf asdfasdf asdfasdf asdfasdfasdfasf
+ </Text>
+ );
+};
diff --git a/packages/design-system/src/components/text.tsx b/packages/design-system/src/components/text.tsx
index 67d77a4b2c..92f4474722 100644
--- a/packages/design-system/src/components/text.tsx
+++ b/packages/design-system/src/components/text.tsx
@@ -80,10 +80,6 @@ export const textStyles = css({
flexBasis: 0,
flexGrow: 1,
-
- // For some reason flexBasis:0 is not enough
- // to stop it from growing past the container
- width: 0,
},
},
},
diff --git a/packages/design-system/src/components/tree/tree-node.tsx b/packages/design-system/src/components/tree/tree-node.tsx
index 66b9c1bf62..1b3ad6ced1 100644
--- a/packages/design-system/src/components/tree/tree-node.tsx
+++ b/packages/design-system/src/components/tree/tree-node.tsx
@@ -47,6 +47,7 @@ const ItemButton = styled("button", {
boxSizing: "border-box",
userSelect: "none",
height: ITEM_HEIGHT,
+ minWidth: 0,
margin: 0,
pt: 0,
pb: 0,