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: Custom attributes / properties (#2912)
Oleg Isonenbe593f7dd015
diff --git a/apps/builder/app/builder/features/settings-panel/controls/text.tsx b/apps/builder/app/builder/features/settings-panel/controls/text.tsxindex d7c764db58..94a17eafee 100644--- a/apps/builder/app/builder/features/settings-panel/controls/text.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/text.tsx@@ -15,6 +15,7 @@ import {$selectedInstanceScope,useBindingState,} from "../shared";+import { useEffect, useRef } from "react";export const TextControl = ({meta,@@ -22,6 +23,7 @@ export const TextControl = ({propName,deletable,computedValue,+ autoFocus,onChange,onDelete,}: ControlProps<"text">) => {@@ -34,6 +36,7 @@ export const TextControl = ({});const id = useId();const label = getLabel(meta, propName);+ const textAreaRef = useRef<HTMLTextAreaElement>(null);const rows = meta.rows ?? 1;const isTwoColumnLayout = rows < 2;@@ -44,9 +47,16 @@ export const TextControl = ({prop?.type === "expression" ? prop.value : undefined);+ useEffect(() => {+ if (autoFocus) {+ textAreaRef.current?.focus();+ }+ }, [autoFocus]);+const input = (<BindingControl><TextArea+ ref={textAreaRef}id={id}disabled={overwritable === false}autoGrowdiff --git a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsxindex 7dccb48f60..45d21f447a 100644--- a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx+++ b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx@@ -33,10 +33,33 @@ import {} from "./use-props-logic";import { Row, getLabel } from "../shared";import { serverSyncStore } from "~/shared/sync";+import { matchSorter } from "match-sorter";const itemToString = (item: NameAndLabel | null) =>item ? getLabel(item, item.name) : "";+const matchOrSuggestToCreate = (+ search: string,+ items: Array<NameAndLabel>,+ itemToString: (item: NameAndLabel) => string+): Array<NameAndLabel> => {+ const matched = matchSorter(items, search, {+ keys: [itemToString],+ });++ if (+ search.trim() !== "" &&+ itemToString(matched[0]).toLocaleLowerCase() !==+ search.toLocaleLowerCase().trim()+ ) {+ matched.unshift({+ name: search.trim(),+ label: `Create "${search.trim()}"`,+ });+ }+ return matched;+};+const PropsCombobox = ({items,onItemSelect,@@ -51,7 +74,7 @@ const PropsCombobox = ({itemToString,onItemSelect,selectedItem: undefined,-+ match: matchOrSuggestToCreate,// this weird handling of value is needed to work around a limitation in useCombobox// where it doesn't allow to leave both `value` and `selectedItem` empty/uncontrolledvalue: { name: "", label: inputValue },@@ -65,11 +88,11 @@ const PropsCombobox = ({<InputFieldautoFocus{...combobox.getInputProps()}- placeholder="New Property"+ placeholder="Find or create a property"suffix={<NestedInputButton {...combobox.getToggleButtonProps()} />}/></ComboboxAnchor>- <ComboboxContent align="end" sideOffset={5}>+ <ComboboxContent align="start" sideOffset={2}><ComboboxListbox {...combobox.getMenuProps()}>{combobox.isOpen &&combobox.items.map((item, index) => (@@ -97,9 +120,10 @@ const renderProperty = (instanceId,}: PropsSectionProps,{ prop, propName, meta }: PropAndMeta,- deletable?: boolean+ { deletable, autoFocus }: { deletable?: boolean; autoFocus?: boolean } = {}) =>renderControl({+ autoFocus,key: propName,instanceId,meta,@@ -195,7 +219,12 @@ export const PropsSection = (props: PropsSectionProps) => {}}/>)}- {logic.addedProps.map((item) => renderProperty(props, item, true))}+ {logic.addedProps.map((item, index) =>+ renderProperty(props, item, {+ deletable: true,+ autoFocus: index === 0,+ })+ )}{logic.initialProps.map((item) => renderProperty(props, item))}</Flex></CollapsibleSectionWithAddButton>diff --git a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.tsindex a17e103fbd..49ef97d626 100644--- a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts+++ b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts@@ -245,28 +245,22 @@ export const usePropsLogic = ({continue;}- let known = getAndDelete(unprocessedKnown, prop.name);-- // @todo:- // if meta is undefined, this means it's a "custom attribute"- // but because custom attributes not implemented yet,- // we'll show it as a regular optional prop for now- if (known === undefined) {- known = getDefaultMetaForType(prop.type);- }+ const meta =+ getAndDelete(unprocessedKnown, prop.name) ??+ getDefaultMetaForType("string");addedProps.push({prop,propName: prop.name,- meta: known,+ meta,});}const handleAdd = (propName: string) => {- const propMeta = unprocessedKnown.get(propName);- if (propMeta === undefined) {- throw new Error(`Attempting to add a prop not lised in availableProps`);- }+ const propMeta =+ unprocessedKnown.get(propName) ??+ // In case of custom property/attribute we get a string.+ getDefaultMetaForType("string");const prop = getStartingProp(instance.id, propMeta, propName);if (prop) {updateProp(prop);diff --git a/apps/builder/app/builder/features/settings-panel/shared.tsx b/apps/builder/app/builder/features/settings-panel/shared.tsxindex 62825ba810..4a77c1858d 100644--- a/apps/builder/app/builder/features/settings-panel/shared.tsx+++ b/apps/builder/app/builder/features/settings-panel/shared.tsx@@ -67,6 +67,7 @@ export type ControlProps<Control> = {deletable: boolean;onChange: (value: PropValue, asset?: Asset) => void;onDelete: () => void;+ autoFocus?: boolean;};export const getLabel = (meta: { label?: string }, fallback: string) =>@@ -138,7 +139,7 @@ export const Label = ({}return (- <Flex align="center" css={{ gap: theme.spacing[3] }}>+ <Flex align="center" css={{ gap: theme.spacing[3], width: "100%" }}>{label}{readOnly && (<Tooltipdiff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsxindex f8d2aeb08f..b05cf6f236 100644--- a/packages/design-system/src/components/combobox.tsx+++ b/packages/design-system/src/components/combobox.tsx@@ -197,6 +197,7 @@ export const useCombobox = <Item,>({...rest}: UseComboboxProps<Item>) => {const [isOpen, setIsOpen] = useState(false);+ const selectedItemRef = useRef<Item>();const { filteredItems, filter, resetFilter } = useFilter<Item>({items,@@ -246,7 +247,9 @@ export const useCombobox = <Item,>({}if (selectedItem != null) {- onItemSelect?.(selectedItem);+ // We are using a ref because we need to call onItemSelect after the component is closed+ // otherwise popover will take focus from on click and you can't focus something else after onItemSelect imediately+ selectedItemRef.current = selectedItem;}},onHighlightedIndexChange({ highlightedIndex }) {@@ -265,6 +268,14 @@ export const useCombobox = <Item,>({}}, [isOpen, resetFilter]);+ useEffect(() => {+ // Selecting the item when the popover is closed.+ if (isOpen === false && selectedItemRef.current) {+ onItemSelect?.(selectedItemRef.current);+ selectedItemRef.current = undefined;+ }+ }, [isOpen, onItemSelect]);+const enhancedGetInputProps = useCallback((options?: UseComboboxGetInputPropsOptions) => {const inputProps = getInputProps(options);