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
Style source input (#776)
Oleg Isonenf9f64934dbf4
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.tsindex 6a267f7d93..59f1d6aeb5 100644--- 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@@ -1,2 +1,2 @@export * from "./style-source-input";-export { type ItemState } from "./style-source";+export { type ItemState, type ItemSource } from "./style-source";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.tsxindex 9cdb86e57a..158f4bdd3f 100644--- 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@@ -1,7 +1,7 @@import { v4 as uuid } from "uuid";import type { ComponentStory } from "@storybook/react";import { useState } from "react";-import { StyleSourceInput, type ItemState } from "./";+import { StyleSourceInput, type ItemState, type ItemSource } from "./";export default {component: StyleSourceInput,@@ -10,40 +10,39 @@ export default {type Item = {id: string;label: string;- source: "token" | "local";+ source: ItemSource;hasMenu: boolean;state: ItemState;};-const localItem: Item = {- id: "0",- label: "Local",- source: "local",- hasMenu: false,- state: "initial",-};-const items: Array<Item> = [{- id: "1",+ id: uuid(),+ label: "Local",+ source: "local",+ hasMenu: false,+ state: "unselected",+ },+ {+ id: uuid(),label: "Apple",source: "token",hasMenu: false,- state: "initial",+ state: "unselected",},{- id: "2",+ id: uuid(),label: "Banana",- source: "token",+ source: "tag",hasMenu: false,- state: "initial",+ state: "unselected",},{- id: "3",+ id: uuid(),label: "Orange",- source: "token",+ source: "state",hasMenu: false,- state: "initial",+ state: "unselected",},];@@ -56,8 +55,8 @@ const createItem = (id: uuid(),label,source: "token",- hasMenu: false,- state: "initial",+ hasMenu: true,+ state: "unselected",};setValue([...value, item]);};@@ -74,7 +73,7 @@ const removeItem = (};export const Basic: ComponentStory<typeof StyleSourceInput> = () => {- const [value, setValue] = useState([localItem, ...items]);+ const [value, setValue] = useState(items);return (<StyleSourceInputcss={{ width: 300 }}@@ -83,7 +82,7 @@ export const Basic: ComponentStory<typeof StyleSourceInput> = () => {onCreateItem={({ label }) => {createItem(label, value, setValue);}}- onSelectItem={(item) => {+ onSelectAutocompleteItem={(item) => {setValue([...value, item]);}}onRemoveItem={(itemToRemove) => {@@ -99,12 +98,12 @@ export const WithTruncatedItem: ComponentStory<> = () => {const [value, setValue] = useState<Array<Item>>([{- id: "0",+ id: uuid(),label:"Local Something Something Something Something Something Something Something Something Something Something Something",source: "local",hasMenu: false,- state: "initial",+ state: "unselected",},]);return (@@ -115,7 +114,7 @@ export const WithTruncatedItem: ComponentStory<onCreateItem={({ label }) => {createItem(label, value, setValue);}}- onSelectItem={(item) => {+ onSelectAutocompleteItem={(item) => {setValue([...value, item]);}}onRemoveItem={(itemToRemove) => {@@ -130,11 +129,11 @@ export const Complete: ComponentStory<typeof StyleSourceInput> = () => {const [value, setValue] = useState<Array<Item>>([...items,{- id: "0",+ id: uuid(),label: "Grape",source: "token",hasMenu: true,- state: "initial",+ state: "selected",},]);const [editingItemId, setEditingItemId] = useState<string>();@@ -148,12 +147,12 @@ export const Complete: ComponentStory<typeof StyleSourceInput> = () => {value={value}editingItemId={editingItemId}currentItemId={currentItemId}- onChangeCurrent={setCurrentItemId}+ onSelectItem={setCurrentItemId}onChangeEditing={setEditingItemId}onCreateItem={({ label }) => {createItem(label, value, setValue);}}- onSelectItem={(item) => {+ onSelectAutocompleteItem={(item) => {setValue([...value, item]);}}onRemoveItem={(itemToRemove) => {@@ -183,7 +182,7 @@ export const Complete: ComponentStory<typeof StyleSourceInput> = () => {setValue(value.map((item) => {if (item.id === itemToEnable.id) {- return { ...item, state: "initial" };+ return { ...item, state: "unselected" };}return item;})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.tsxindex 412e2779fd..0ff10c8496 100644--- 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@@ -19,7 +19,7 @@ import {type RefObject,} from "react";import { mergeRefs } from "@react-aria/utils";-import { StyleSource, type ItemState } from "./style-source";+import { ItemSource, StyleSource, type ItemState } from "./style-source";import { useSortable } from "./use-sortable";type IntermediateItem = {@@ -27,6 +27,7 @@ type IntermediateItem = {label: string;hasMenu: boolean;state: ItemState;+ source: ItemSource;};type TextFieldBaseWrapperProps<Item> = Omit<ComponentProps<"input">, "value"> &@@ -45,7 +46,7 @@ type TextFieldBaseWrapperProps<Item> = Omit<ComponentProps<"input">, "value"> &onDisableItem?: (item: Item) => void;onEnableItem?: (item: Item) => void;onSort?: (items: Array<Item>) => void;- onChangeCurrent?: (id: string) => void;+ onSelectItem?: (id: string) => void;onChangeEditing?: (id?: string) => void;editingItemId?: string;currentItemId?: string;@@ -61,7 +62,7 @@ const TextFieldBase: ForwardRefRenderFunction<containerRef,inputRef,state,- variant,+ variant: textFieldVariant,onFocus,onBlur,onClick,@@ -75,7 +76,7 @@ const TextFieldBase: ForwardRefRenderFunction<onDisableItem,onEnableItem,onSort,- onChangeCurrent,+ onSelectItem,onChangeEditing,currentItemId,editingItemId,@@ -97,7 +98,7 @@ const TextFieldBase: ForwardRefRenderFunction<aria-disabled={disabled}ref={mergeRefs(forwardedRef, containerRef ?? null, sortableRefCallback)}state={state}- variant={variant}+ variant={textFieldVariant}css={{ ...css, px: "$spacing$3", py: "$spacing$2" }}onKeyDown={onKeyDown}>@@ -111,18 +112,18 @@ const TextFieldBase: ForwardRefRenderFunction<? "editing": item.state}- isCurrent={item.id === currentItemId}+ source={item.source}onStateChange={(state) => {onChangeEditing?.(state === "editing" ? item.id : undefined);if (state === "disabled") {onDisableItem?.(item);}- if (state === "initial") {+ if (state === "unselected") {onEnableItem?.(item);}}}- onChangeCurrent={() => {- onChangeCurrent?.(item.id);+ onSelect={() => {+ onSelectItem?.(item.id);}}onChange={(label) => {onChangeEditing?.();@@ -164,11 +165,11 @@ type StyleSourceInputProps<Item> = {value?: Array<Item>;editingItemId?: string;currentItemId?: string;- onSelectItem?: (item: Item) => void;+ onSelectAutocompleteItem?: (item: Item) => void;onRemoveItem?: (item: Item) => void;onCreateItem?: (item: Item) => void;onChangeItem?: (item: Item) => void;- onChangeCurrent?: (id: string) => void;+ onSelectItem?: (id: string) => void;onChangeEditing?: (id?: string) => void;onDuplicateItem?: (item: Item) => void;onDisableItem?: (item: Item) => void;@@ -191,12 +192,18 @@ export const StyleSourceInput = <Item extends IntermediateItem>(isOpen,} = useCombobox({items: props.items ?? [],- value: { label, hasMenu: true, state: "initial", id: "" },+ value: {+ label,+ hasMenu: true,+ state: "unselected",+ id: "",+ source: "local",+ },selectedItem: undefined,itemToString: (item) => (item ? item.label : ""),onItemSelect(item) {setLabel("");- props.onSelectItem?.(item as Item);+ props.onSelectAutocompleteItem?.(item as Item);},onInputChange(label) {setLabel(label ?? "");@@ -224,7 +231,7 @@ export const StyleSourceInput = <Item extends IntermediateItem>({...inputProps}onRemoveItem={props.onRemoveItem}onChangeItem={props.onChangeItem}- onChangeCurrent={props.onChangeCurrent}+ onSelectItem={props.onSelectItem}onChangeEditing={props.onChangeEditing}onDuplicateItem={props.onDuplicateItem}onDisableItem={props.onDisableItem}diff --git a/apps/designer/app/designer/features/style-panel/style-source-input/style-source.tsx b/apps/designer/app/designer/features/style-panel/style-source-input/style-source.tsxindex 82dfd02833..e7a2c96ea4 100644--- a/apps/designer/app/designer/features/style-panel/style-source-input/style-source.tsx+++ b/apps/designer/app/designer/features/style-panel/style-source-input/style-source.tsx@@ -6,7 +6,6 @@ import {DropdownMenuPortal,DropdownMenuTrigger,IconButton,- DeprecatedButton,Text,styled,} from "@webstudio-is/design-system";@@ -93,49 +92,83 @@ const Menu = (props: MenuProps) => {);};-export type ItemState = "initial" | "editing" | "disabled" | "dragging";+export type ItemState =+ | "unselected"+ | "selected"+ | "editing"+ | "disabled"+ | "dragging";++export type ItemSource = "token" | "tag" | "state" | "local";const useEditableText = ({state,onStateChange,onChange,+ onClick,}: {state: ItemState;onStateChange: (state: ItemState) => void;onChange: (value: string) => void;+ onClick: () => void;}) => {- const ref = useRef<HTMLDivElement>(null);+ const elementRef = useRef<HTMLDivElement>(null);+ const lastValueRef = useRef<string>("");+ const lastStateRef = useRef<ItemState>(state);+ const getValue = () => elementRef.current?.textContent ?? "";useEffect(() => {- if (ref.current === null) {+ if (elementRef.current === null) {return;}if (state === "editing") {- ref.current.setAttribute("contenteditable", "plaintext-only");- ref.current.focus();- getSelection()?.selectAllChildren(ref.current);+ elementRef.current.setAttribute("contenteditable", "plaintext-only");+ elementRef.current.focus();+ getSelection()?.selectAllChildren(elementRef.current);+ lastValueRef.current = getValue();return;}- ref.current?.removeAttribute("contenteditable");+ elementRef.current?.removeAttribute("contenteditable");+ lastStateRef.current = state;}, [state]);const handleFinishEditing = (event: KeyboardEvent<Element> | FocusEvent<Element>) => {+ if (state !== "editing" || elementRef.current === null) {+ return;+ }event.preventDefault();- onStateChange("initial");- onChange(ref.current?.textContent ?? "");+ onStateChange(lastStateRef.current);+ // Reverting to the previous value when user hits Escape+ if ("key" in event && event.key === "Escape") {+ elementRef.current.textContent = lastValueRef.current;+ return;+ }+ onChange(getValue());+ lastValueRef.current = "";};const handleKeyDown: KeyboardEventHandler = (event) => {- if (event.key === "Enter") {+ if (event.key === "Enter" || event.key === "Escape") {handleFinishEditing(event);}};- return { handleKeyDown, handleFinishEditing, ref };+ const handleDoubleClick = () => {+ onStateChange("editing");+ };++ const handlers = {+ onKeyDown: handleKeyDown,+ onBlur: handleFinishEditing,+ onClick,+ onDoubleClick: handleDoubleClick,+ };++ return { ref: elementRef, handlers };};type EditableTextProps = {@@ -143,7 +176,7 @@ type EditableTextProps = {onChange: (value: string) => void;state: ItemState;onStateChange: (state: ItemState) => void;- onChangeCurrent: () => void;+ onClick: () => void;};const EditableText = ({@@ -151,25 +184,24 @@ const EditableText = ({label,state,onStateChange,- onChangeCurrent,+ onClick,}: EditableTextProps) => {- const { ref, handleKeyDown, handleFinishEditing } = useEditableText({+ const { ref, handlers } = useEditableText({state,onStateChange,onChange,+ onClick,});return (<Texttruncateref={ref}- onKeyDown={handleKeyDown}- onBlur={handleFinishEditing}- onClick={onChangeCurrent}css={{outline: "none",textOverflow: state === "editing" ? "clip" : "ellipsis",}}+ {...handlers}>{label}</Text>@@ -201,65 +233,59 @@ const useForceRecalcStyle = <Element extends HTMLElement>(return ref;};-const Item = styled(DeprecatedButton, {+const Item = styled("div", {+ display: "inline-flex",+ borderRadius: "$borderRadius$3",+ padding: "$spacing$4",maxWidth: "100%",+ minWidth: 0,position: "relative",+ color: "$colors$foregroundContrastMain",...menuCssVars({ show: false }),"&:hover": menuCssVars({ show: true }),variants: {+ source: {+ local: {+ backgroundColor: "$colors$backgroundStyleSourceToken",+ },+ token: {+ backgroundColor: "$colors$backgroundStyleSourceToken",+ },+ tag: {+ backgroundColor: "$colors$backgroundStyleSourceTag",+ },+ state: {+ backgroundColor: "$colors$backgroundStyleSourceState",+ },+ },state: {+ selected: {},+ unselected: {+ backgroundColor: "$colors$backgroundStyleSourceNeutral",+ },disabled: {// @todo stylingopacity: 0.5,},editing: {},- initial: {},dragging: {- // @todo styling- opacity: 0.5,+ backgroundColor: "$colors$backgroundStyleSourceDisabled",},},},+ defaultVariants: {+ state: "unselected",+ },});-type EditableItemProps = {- id: string;- children: Array<JSX.Element | false>;- state: ItemState;- isCurrent: boolean;-};--const EditableItem = ({- children,- state,- id,- isCurrent,-}: EditableItemProps) => {- const ref = useForceRecalcStyle<HTMLDivElement>(- "max-width",- state === "editing"- );- return (- <Item- variant={isCurrent ? "blue" : "gray"}- state={state}- ref={ref}- as="div"- data-id={id}- >- {children}- </Item>- );-};-type StyleSourceProps = {id: string;label: string;hasMenu: boolean;state: ItemState;- isCurrent: boolean;+ source: ItemSource;onStateChange: (state: ItemState) => void;- onChangeCurrent: () => void;+ onSelect: () => void;onDuplicate: () => void;onRemove: () => void;onChange: (value: string) => void;@@ -270,19 +296,23 @@ export const StyleSource = ({label,hasMenu,state,- isCurrent,+ source,onChange,onStateChange,- onChangeCurrent,+ onSelect,onDuplicate,onRemove,}: StyleSourceProps) => {+ const ref = useForceRecalcStyle<HTMLDivElement>(+ "max-width",+ state === "editing"+ );return (- <EditableItem state={state} id={id} isCurrent={isCurrent}>+ <Item state={state} source={source} data-id={id} ref={ref}><EditableTextstate={state}onStateChange={onStateChange}- onChangeCurrent={onChangeCurrent}+ onClick={onSelect}onChange={onChange}label={label}/>@@ -292,7 +322,7 @@ export const StyleSource = ({onDuplicate={onDuplicate}onRemove={onRemove}onEnable={() => {- onStateChange("initial");+ onStateChange("unselected");}}onDisable={() => {onStateChange("disabled");@@ -302,6 +332,6 @@ export const StyleSource = ({}}/>)}- </EditableItem>+ </Item>);};