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.

fix: Prevent duplicate token when renaming (#3873)

Oleg Isonen2fb558bfd322
diff --git a/apps/builder/app/builder/features/address-bar.tsx b/apps/builder/app/builder/features/address-bar.tsx
index 6c1f715280..ea40ee8b89 100644
--- a/apps/builder/app/builder/features/address-bar.tsx
+++ b/apps/builder/app/builder/features/address-bar.tsx
@@ -21,7 +21,6 @@ import {
ToolbarButton,
Popover,
PopoverTrigger,
- PopoverPortal,
PopoverContent,
IconButton,
MenuItemButton,
@@ -421,16 +420,9 @@ export const AddressBarPopover = () => {
<DynamicPageIcon />
</ToolbarButton>
</PopoverTrigger>
- <PopoverPortal>
- <PopoverContent
- css={{ padding: 0 }}
- sideOffset={0}
- collisionPadding={4}
- align="start"
- >
- <AddressBar ref={formRef} onSubmit={() => setIsOpen(false)} />
- </PopoverContent>
- </PopoverPortal>
+ <PopoverContent sideOffset={0} collisionPadding={4} align="start">
+ <AddressBar ref={formRef} onSubmit={() => setIsOpen(false)} />
+ </PopoverContent>
</Popover>
);
};
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx b/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
index 359a5bfd85..e38ce0aac9 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
+++ b/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
@@ -90,7 +90,7 @@ const BreakpointEditorItem = ({
useHandleChangeComplete(breakpoint, onChangeComplete);
return (
- <Flex gap="2" css={{ mx: theme.spacing[7] }}>
+ <Flex gap="2">
<form
ref={formRef}
onKeyPress={(event) => {
@@ -186,9 +186,7 @@ export const BreakpointsEditor = ({ onDelete }: BreakpointsEditorProps) => {
return (
<Flex direction="column">
<PanelTitle
- css={{
- px: theme.spacing[7],
- }}
+ css={{ px: theme.spacing[9] }}
suffix={
<IconButton
onClick={() => {
@@ -207,21 +205,23 @@ export const BreakpointsEditor = ({ onDelete }: BreakpointsEditorProps) => {
{"Breakpoints"}
</PanelTitle>
<Separator />
- <Box css={{ marginTop: theme.spacing[5] }}>
+ <Fragment>
{allBreakpoints.map((breakpoint, index, all) => {
return (
<Fragment key={breakpoint.id}>
- <BreakpointEditorItem
- breakpoint={breakpoint}
- onChangeComplete={handleChangeComplete}
- onDelete={onDelete}
- autoFocus={index === 0}
- />
+ <Box css={{ px: theme.spacing[7], py: theme.spacing[5] }}>
+ <BreakpointEditorItem
+ breakpoint={breakpoint}
+ onChangeComplete={handleChangeComplete}
+ onDelete={onDelete}
+ autoFocus={index === 0}
+ />
+ </Box>
{index < all.length - 1 && <PopoverSeparator />}
</Fragment>
);
})}
- </Box>
+ </Fragment>
{allBreakpoints.length === 0 && (
<Text css={{ margin: theme.spacing[10] }}>No breakpoints found</Text>
)}
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoints-popover.tsx b/apps/builder/app/builder/features/breakpoints/breakpoints-popover.tsx
index 8a8f045f06..b28ba5c080 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoints-popover.tsx
+++ b/apps/builder/app/builder/features/breakpoints/breakpoints-popover.tsx
@@ -8,7 +8,6 @@ import {
Label,
Button,
Popover,
- PopoverPortal,
PopoverContent,
PopoverTrigger,
MenuCheckedIcon,
@@ -17,7 +16,6 @@ import {
ListItem,
MenuItemButton,
Box,
- PopoverMenuItemContainer,
PopoverMenuItemRightSlot,
Tooltip,
} from "@webstudio-is/design-system";
@@ -94,121 +92,113 @@ export const BreakpointsPopover = () => {
<BreakpointsPopoverToolbarButton css={{ gap: theme.spacing[5] }} />
</PopoverTrigger>
</Tooltip>
- <PopoverPortal>
- <PopoverContent
- css={{ padding: 0 }}
- sideOffset={0}
- collisionPadding={4}
- align="start"
- >
- {view === "confirmation" && breakpointToDelete && (
- <ConfirmationDialog
- breakpoint={breakpointToDelete}
- onAbort={() => {
- setBreakpointToDelete(undefined);
- $breakpointsMenuView.set("editor");
- }}
- onConfirm={handleDelete}
- />
- )}
- {view === "editor" && (
- <BreakpointsEditor
- onDelete={(breakpoint) => {
- setBreakpointToDelete(breakpoint);
- $breakpointsMenuView.set("confirmation");
- }}
- />
- )}
- {view === "initial" && (
- <>
+ <PopoverContent sideOffset={0} collisionPadding={4} align="start">
+ {view === "confirmation" && breakpointToDelete && (
+ <ConfirmationDialog
+ breakpoint={breakpointToDelete}
+ onAbort={() => {
+ setBreakpointToDelete(undefined);
+ $breakpointsMenuView.set("editor");
+ }}
+ onConfirm={handleDelete}
+ />
+ )}
+ {view === "editor" && (
+ <BreakpointsEditor
+ onDelete={(breakpoint) => {
+ setBreakpointToDelete(breakpoint);
+ $breakpointsMenuView.set("confirmation");
+ }}
+ />
+ )}
+ {view === "initial" && (
+ <>
+ <Flex css={{ px: theme.spacing[7], py: theme.spacing[5] }} gap="3">
+ <WidthInput min={minCanvasWidth} />
+ <Flex align="center" gap="2">
+ <Label>Scale</Label>
+ <Button
+ color="neutral"
+ css={{ width: theme.spacing[17] }}
+ tabIndex={-1}
+ >
+ {Math.round(scale)}%
+ </Button>
+ </Flex>
+ </Flex>
+ <PopoverSeparator />
+ <List asChild>
<Flex
- css={{ px: theme.spacing[7], paddingTop: theme.spacing[5] }}
- gap="3"
+ direction="column"
+ css={{ px: theme.spacing[3], py: theme.spacing[5] }}
>
- <WidthInput min={minCanvasWidth} />
- <Flex align="center" gap="2">
- <Label>Scale</Label>
- <Button
- color="neutral"
- css={{ width: theme.spacing[17] }}
- tabIndex={-1}
- >
- {Math.round(scale)}%
- </Button>
- </Flex>
+ {groupBreakpoints(Array.from(breakpoints.values())).map(
+ (breakpoint, index) => {
+ return (
+ <ListItem
+ asChild
+ onSelect={() => {
+ $selectedBreakpointId.set(breakpoint.id);
+ setCanvasWidth(breakpoint.id);
+ }}
+ index={index}
+ key={breakpoint.id}
+ >
+ <MenuItemButton withIndicator>
+ {breakpoint === selectedBreakpoint && (
+ <MenuItemIndicator>
+ <MenuCheckedIcon />
+ </MenuItemIndicator>
+ )}
+ <Box
+ css={{ flexGrow: 1, textAlign: "left" }}
+ as="span"
+ >
+ {breakpoint.label}
+ </Box>
+ <PopoverMenuItemRightSlot
+ css={{ color: theme.colors.foregroundSubtle }}
+ >
+ {breakpoint.minWidth !== undefined
+ ? `≥ ${breakpoint.minWidth} PX`
+ : breakpoint.maxWidth !== undefined
+ ? `≤ ${breakpoint.maxWidth} PX`
+ : "All Sizes"}
+ </PopoverMenuItemRightSlot>
+ </MenuItemButton>
+ </ListItem>
+ );
+ }
+ )}
</Flex>
- <PopoverSeparator />
- <List asChild>
- <Flex direction="column" css={{ my: 0, mx: theme.spacing[3] }}>
- {groupBreakpoints(Array.from(breakpoints.values())).map(
- (breakpoint, index) => {
- return (
- <ListItem
- asChild
- onSelect={() => {
- $selectedBreakpointId.set(breakpoint.id);
- setCanvasWidth(breakpoint.id);
- }}
- index={index}
- key={breakpoint.id}
- >
- <MenuItemButton withIndicator>
- {breakpoint === selectedBreakpoint && (
- <MenuItemIndicator>
- <MenuCheckedIcon />
- </MenuItemIndicator>
- )}
- <Box
- css={{ flexGrow: 1, textAlign: "left" }}
- as="span"
- >
- {breakpoint.label}
- </Box>
- <PopoverMenuItemRightSlot
- css={{ color: theme.colors.foregroundSubtle }}
- >
- {breakpoint.minWidth !== undefined
- ? `≥ ${breakpoint.minWidth} PX`
- : breakpoint.maxWidth !== undefined
- ? `≤ ${breakpoint.maxWidth} PX`
- : "All Sizes"}
- </PopoverMenuItemRightSlot>
- </MenuItemButton>
- </ListItem>
- );
- }
- )}
- </Flex>
- </List>
- </>
- )}
- {(view === "editor" || view === "initial") && (
- <>
- <PopoverSeparator />
- <PopoverMenuItemContainer
- css={{
- justifyContent: "center",
- mx: theme.spacing[7],
- paddingBottom: theme.spacing[5],
+ </List>
+ </>
+ )}
+ {(view === "editor" || view === "initial") && (
+ <>
+ <PopoverSeparator />
+ <Flex
+ css={{
+ justifyContent: "center",
+ padding: theme.spacing[5],
+ }}
+ >
+ <Button
+ color="neutral"
+ css={{ flexGrow: 1 }}
+ onClick={(event) => {
+ event.preventDefault();
+ $breakpointsMenuView.set(
+ view === "initial" ? "editor" : "initial"
+ );
}}
>
- <Button
- color="neutral"
- css={{ flexGrow: 1 }}
- onClick={(event) => {
- event.preventDefault();
- $breakpointsMenuView.set(
- view === "initial" ? "editor" : "initial"
- );
- }}
- >
- {view === "editor" ? "Done" : "Edit breakpoints"}
- </Button>
- </PopoverMenuItemContainer>
- </>
- )}
- </PopoverContent>
- </PopoverPortal>
+ {view === "editor" ? "Done" : "Edit breakpoints"}
+ </Button>
+ </Flex>
+ </>
+ )}
+ </PopoverContent>
</Popover>
);
};
diff --git a/apps/builder/app/builder/features/sidebar-left/help-popover.tsx b/apps/builder/app/builder/features/sidebar-left/help-popover.tsx
index 92cd9624b0..38102c9fef 100644
--- a/apps/builder/app/builder/features/sidebar-left/help-popover.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/help-popover.tsx
@@ -3,7 +3,6 @@ import {
Flex,
Popover,
PopoverContent,
- PopoverPortal,
PopoverTrigger,
rawTheme,
theme,
@@ -28,65 +27,63 @@ export const HelpPopover = ({
return (
<Popover {...popoverProps}>
{children}
- <PopoverPortal>
- <PopoverContent
- avoidCollisions
- sideOffset={0}
- // Height of the footer
- collisionPadding={{ bottom: Number.parseFloat(rawTheme.spacing[11]) }}
- side={side}
+ <PopoverContent
+ avoidCollisions
+ sideOffset={0}
+ // Height of the footer
+ collisionPadding={{ bottom: Number.parseFloat(rawTheme.spacing[11]) }}
+ side={side}
+ >
+ <Flex
+ as="form"
+ target="_blank"
+ direction="column"
+ css={{ padding: theme.spacing[5] }}
+ gap="2"
>
- <Flex
- as="form"
- target="_blank"
- direction="column"
- css={{ px: theme.spacing[7], py: theme.spacing[3] }}
- gap="2"
+ <Button
+ formAction="https://www.youtube.com/playlist"
+ name="list"
+ value="PL4vVqpngzeT4sDlanyPe99dYl8BgUYCac"
+ prefix={<Youtube1cIcon />}
+ color="destructive"
>
- <Button
- formAction="https://www.youtube.com/playlist"
- name="list"
- value="PL4vVqpngzeT4sDlanyPe99dYl8BgUYCac"
- prefix={<Youtube1cIcon />}
- color="destructive"
- >
- Learn with videos
- </Button>
- <Button
- formAction="https://docs.webstudio.is/"
- prefix={<Webstudio1cIcon />}
- color="gradient"
- >
- Learn from docs
- </Button>
- <Button
- formAction="https://discord.gg/UNdyrDkq5r"
- prefix={<DiscordIcon />}
- color="primary"
- >
- Join the Community
- </Button>
- <Button
- formAction="https://github.com/webstudio-is/webstudio-community/discussions"
- prefix={<GithubIcon />}
- color="dark"
- >
- Discuss on GitHub
- </Button>
- <Button
- prefix={<BugIcon />}
- color="dark"
- onClick={() => {
- window.open(
- "https://github.com/webstudio-is/webstudio-community/discussions/new?category=q-a&labels=bug&title=[Bug]"
- );
- }}
- >
- Report a bug
- </Button>
- </Flex>
- </PopoverContent>
- </PopoverPortal>
+ Learn with videos
+ </Button>
+ <Button
+ formAction="https://docs.webstudio.is/"
+ prefix={<Webstudio1cIcon />}
+ color="gradient"
+ >
+ Learn from docs
+ </Button>
+ <Button
+ formAction="https://discord.gg/UNdyrDkq5r"
+ prefix={<DiscordIcon />}
+ color="primary"
+ >
+ Join the Community
+ </Button>
+ <Button
+ formAction="https://github.com/webstudio-is/webstudio-community/discussions"
+ prefix={<GithubIcon />}
+ color="dark"
+ >
+ Discuss on GitHub
+ </Button>
+ <Button
+ prefix={<BugIcon />}
+ color="dark"
+ onClick={() => {
+ window.open(
+ "https://github.com/webstudio-is/webstudio-community/discussions/new?category=q-a&labels=bug&title=[Bug]"
+ );
+ }}
+ >
+ Report a bug
+ </Button>
+ </Flex>
+ </PopoverContent>
</Popover>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/style-source-section.tsx b/apps/builder/app/builder/features/style-panel/style-source-section.tsx
index 38354a4453..9ed450e9d4 100644
--- a/apps/builder/app/builder/features/style-panel/style-source-section.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-source-section.tsx
@@ -21,7 +21,11 @@ import {
Text,
theme,
} from "@webstudio-is/design-system";
-import { type ItemSource, StyleSourceInput } from "./style-source";
+import {
+ type ItemSource,
+ StyleSourceInput,
+ type StyleSourceError,
+} from "./style-source";
import {
$breakpoints,
$instances,
@@ -363,11 +367,27 @@ const reorderStyleSources = (styleSourceIds: StyleSource["id"][]) => {
);
};
-const renameStyleSource = (id: StyleSource["id"], label: string) => {
+const renameStyleSource = (
+ id: StyleSource["id"],
+ name: string
+): StyleSourceError | undefined => {
+ const styleSources = $styleSources.get();
+ if (name.trim().length === 0) {
+ return { type: "minlength", id };
+ }
+ for (const styleSource of styleSources.values()) {
+ if (
+ styleSource.type === "token" &&
+ styleSource.name === name &&
+ styleSource.id !== id
+ ) {
+ return { type: "duplicate", id };
+ }
+ }
serverSyncStore.createTransaction([$styleSources], (styleSources) => {
const styleSource = styleSources.get(id);
if (styleSource?.type === "token") {
- styleSource.name = label;
+ styleSource.name = name;
}
});
};
@@ -481,15 +501,23 @@ export const StyleSourcesSection = () => {
$selectedOrLastStyleSourceSelector
);
- const [editingItemId, setEditingItemId] = useState<
- undefined | StyleSource["id"]
- >(undefined);
+ const [editingItemId, setEditingItemId] = useState<StyleSource["id"]>();
const [tokenToDelete, setTokenToDelete] = useState<StyleSourceToken>();
+ const [error, setError] = useState<StyleSourceError>();
+
+ const setEditingItem = (id?: StyleSource["id"]) => {
+ // User finished editing or started editing a different token
+ if (error && (id === undefined || id !== error.id)) {
+ setError(undefined);
+ }
+ setEditingItemId(id);
+ };
return (
<>
<StyleSourceInput
+ error={error}
items={availableStyleSources}
value={value}
selectedItemSelector={selectedOrLastStyleSourceSelector}
@@ -501,12 +529,12 @@ export const StyleSourcesSection = () => {
onDuplicateItem={(id) => {
const newId = duplicateStyleSource(id);
if (newId !== undefined) {
- setEditingItemId(newId);
+ setEditingItem(newId);
}
}}
onConvertToToken={(id) => {
convertLocalStyleSourceToToken(id);
- setEditingItemId(id);
+ setEditingItem(id);
}}
onClearStyles={clearStyles}
onRemoveItem={(id) => {
@@ -531,14 +559,20 @@ export const StyleSourcesSection = () => {
// style source renaming
editingItemId={editingItemId}
onEditItem={(id) => {
- setEditingItemId(id);
+ setEditingItem(id);
// prevent deselect after renaming
if (id !== undefined) {
selectStyleSource(id);
}
}}
onChangeItem={(item) => {
- renameStyleSource(item.id, item.label);
+ const error = renameStyleSource(item.id, item.label);
+ if (error) {
+ setError(error);
+ setEditingItem(item.id);
+ return;
+ }
+ setError(undefined);
}}
/>
<DeleteConfirmationDialog
diff --git a/apps/builder/app/builder/features/style-panel/style-source/index.ts b/apps/builder/app/builder/features/style-panel/style-source/index.ts
index c0ad1de8fc..57d1c6f8c8 100644
--- a/apps/builder/app/builder/features/style-panel/style-source/index.ts
+++ b/apps/builder/app/builder/features/style-panel/style-source/index.ts
@@ -1,3 +1,3 @@
export * from "./style-source-input";
export * from "./style-source-badge";
-export type { ItemSource } from "./style-source";
+export type { ItemSource, StyleSourceError } from "./style-source-control";
diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-control.tsx
similarity index 77%
rename from apps/builder/app/builder/features/style-panel/style-source/style-source.tsx
rename to apps/builder/app/builder/features/style-panel/style-source/style-source-control.tsx
index e88e7615e2..3399f4375e 100644
--- a/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-control.tsx
@@ -8,8 +8,10 @@ import {
Box,
theme,
Flex,
+ Tooltip,
} from "@webstudio-is/design-system";
import { ChevronDownIcon } from "@webstudio-is/icons";
+import type { StyleSource } from "@webstudio-is/sdk";
import { type ReactNode } from "react";
import { useContentEditable } from "~/shared/dom-hooks";
@@ -110,14 +112,14 @@ const Menu = (props: MenuProps) => {
export type ItemSource = "token" | "componentToken" | "tag" | "local";
type EditableTextProps = {
- label: string;
+ value: string;
isEditing: boolean;
onChangeEditing: (isEditing: boolean) => void;
onChangeValue: (value: string) => void;
};
const EditableText = ({
- label,
+ value,
isEditing,
onChangeEditing,
onChangeValue,
@@ -127,6 +129,7 @@ const EditableText = ({
isEditing,
onChangeEditing,
onChangeValue,
+ value,
});
return (
@@ -141,9 +144,7 @@ const EditableText = ({
cursor: isEditing ? "auto" : "default",
}}
{...handlers}
- >
- {label}
- </Text>
+ />
);
};
@@ -152,7 +153,10 @@ const StyleSourceContainer = styled(Box, {
borderRadius: theme.borderRadius[3],
minWidth: theme.spacing[14],
maxWidth: "100%",
+ height: theme.spacing[10],
position: "relative",
+ overflow: "hidden",
+ alignItems: "center",
color: theme.colors.foregroundContrastMain,
...menuCssVars({ show: false }),
"&:hover": menuCssVars({ show: true }),
@@ -197,6 +201,11 @@ const StyleSourceContainer = styled(Box, {
},
false: {},
},
+ hasError: {
+ true: {
+ backgroundColor: theme.colors.backgroundDestructiveMain,
+ },
+ },
},
});
@@ -240,8 +249,19 @@ const StyleSourceState = styled(Text, {
},
});
-type StyleSourceProps = {
- id: string;
+const errors = {
+ minlength: "Token must be at least 1 character long",
+ duplicate: "Token already exists",
+} as const;
+
+export type StyleSourceError = {
+ type: keyof typeof errors;
+ id: StyleSource["id"];
+};
+
+type StyleSourceControlProps = {
+ id: StyleSource["id"];
+ error?: StyleSourceError;
children: ReactNode;
menuItems: ReactNode;
selected: boolean;
@@ -256,12 +276,13 @@ type StyleSourceProps = {
onChangeEditing: (isEditing: boolean) => void;
};
-export const StyleSource = ({
+export const StyleSourceControl = ({
id,
menuItems,
selected,
state,
stateLabel,
+ error,
disabled,
isEditing,
isDragging,
@@ -270,43 +291,47 @@ export const StyleSource = ({
onChangeValue,
onChangeEditing,
onSelect,
-}: StyleSourceProps) => {
+}: StyleSourceControlProps) => {
const showMenu = isEditing === false && isDragging === false;
return (
- <StyleSourceContainer
- data-id={id}
- source={source}
- selected={selected && state === undefined}
- disabled={disabled}
- aria-current={selected && state === undefined}
- role="button"
+ <Tooltip
+ content={error ? errors[error.type] : ""}
+ open={error !== undefined}
>
- <Flex css={{ flexGrow: 1, py: theme.spacing[2], px: theme.spacing[3] }}>
- <StyleSourceButton
- disabled={disabled || isEditing}
- isEditing={isEditing}
- onClick={onSelect}
- tabIndex={-1}
- >
- {typeof children === "string" ? (
- <EditableText
- isEditing={isEditing}
- onChangeEditing={onChangeEditing}
- onChangeValue={onChangeValue}
- label={children}
- />
- ) : (
- children
- )}
- </StyleSourceButton>
- </Flex>
- {stateLabel !== undefined && (
- <StyleSourceState source={source} css={{ lineHeight: 1 }}>
- {stateLabel}
- </StyleSourceState>
- )}
- {showMenu && <Menu>{menuItems}</Menu>}
- </StyleSourceContainer>
+ <StyleSourceContainer
+ data-id={id}
+ source={source}
+ selected={selected && state === undefined}
+ disabled={disabled}
+ aria-current={selected && state === undefined}
+ role="button"
+ hasError={error !== undefined}
+ >
+ <Flex grow css={{ py: theme.spacing[2], px: theme.spacing[3] }}>
+ <StyleSourceButton
+ disabled={disabled || isEditing}
+ isEditing={isEditing}
+ onClick={onSelect}
+ tabIndex={-1}
+ >
+ {typeof children === "string" ? (
+ <EditableText
+ isEditing={isEditing}
+ onChangeEditing={onChangeEditing}
+ onChangeValue={onChangeValue}
+ value={children}
+ />
+ ) : (
+ children
+ )}
+ </StyleSourceButton>
+ </Flex>
+ {stateLabel !== undefined && (
+ <StyleSourceState source={source}>{stateLabel}</StyleSourceState>
+ )}
+ {showMenu && <Menu>{menuItems}</Menu>}
+ </StyleSourceContainer>
+ </Tooltip>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx
index aeb40f684d..0b2023a395 100644
--- a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx
@@ -47,7 +47,12 @@ import {
} from "react";
import { mergeRefs } from "@react-aria/utils";
import { type ComponentState, stateCategories } from "@webstudio-is/react-sdk";
-import { type ItemSource, menuCssVars, StyleSource } from "./style-source";
+import {
+ type ItemSource,
+ type StyleSourceError,
+ menuCssVars,
+ StyleSourceControl,
+} from "./style-source-control";
import { useSortable } from "./use-sortable";
import { matchSorter } from "match-sorter";
import { StyleSourceBadge } from "./style-source-badge";
@@ -102,6 +107,7 @@ type TextFieldBaseWrapperProps<Item extends IntermediateItem> = Omit<
onEditItem?: (id?: Item["id"]) => void;
editingItemId?: Item["id"];
states: { label: string; selector: string }[];
+ error?: StyleSourceError;
};
const TextFieldBase: ForwardRefRenderFunction<
@@ -126,6 +132,7 @@ const TextFieldBase: ForwardRefRenderFunction<
onEditItem,
editingItemId,
states,
+ error,
...textFieldProps
} = props;
const { sortableRefCallback, dragItemId, placementIndicator } = useSortable({
@@ -187,7 +194,7 @@ const TextFieldBase: ForwardRefRenderFunction<
/>
)}
{value.map((item) => (
- <StyleSource
+ <StyleSourceControl
key={item.id}
menuItems={renderStyleSourceMenuItems(item)}
id={item.id}
@@ -199,10 +206,12 @@ const TextFieldBase: ForwardRefRenderFunction<
}
stateLabel={
item.id === selectedItemSelector?.styleSourceId
- ? states.find((s) => s.selector === selectedItemSelector.state)
- ?.label
+ ? states.find(
+ (state) => state.selector === selectedItemSelector.state
+ )?.label
: undefined
}
+ error={item.id === error?.id ? error : undefined}
disabled={item.disabled}
isDragging={item.id === dragItemId}
isEditing={item.id === editingItemId}
@@ -223,7 +232,7 @@ const TextFieldBase: ForwardRefRenderFunction<
) : (
item.label
)}
- </StyleSource>
+ </StyleSourceControl>
))}
{placementIndicator}
</TextFieldContainer>
@@ -234,6 +243,7 @@ const TextField = forwardRef(TextFieldBase);
TextField.displayName = "TextField";
type StyleSourceInputProps<Item extends IntermediateItem> = {
+ error?: StyleSourceError;
items?: Array<Item>;
value?: Array<Item>;
selectedItemSelector: undefined | ItemSelector;
@@ -322,7 +332,7 @@ const renderMenuItems = (props: {
<DropdownMenuLabel>{props.item.label}</DropdownMenuLabel>
{props.item.source !== "local" && (
<DropdownMenuItem onSelect={() => props.onEdit?.(props.item.id)}>
- Edit Name
+ Rename
</DropdownMenuItem>
)}
{props.item.source !== "local" && (
@@ -504,6 +514,7 @@ export const StyleSourceInput = (
<TextField
// @todo inputProps is any which breaks all types passed to TextField
{...inputProps}
+ error={props.error}
renderStyleSourceMenuItems={(item) =>
renderMenuItems({
selectedItemSelector: props.selectedItemSelector,
diff --git a/apps/builder/app/builder/features/style-panel/style-source/use-sortable.tsx b/apps/builder/app/builder/features/style-panel/style-source/use-sortable.tsx
index 719cc8ac61..630c4f3520 100644
--- a/apps/builder/app/builder/features/style-panel/style-source/use-sortable.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-source/use-sortable.tsx
@@ -10,7 +10,7 @@ import {
computeIndicatorPlacement,
toast,
} from "@webstudio-is/design-system";
-import type { ItemSource } from "./style-source";
+import type { ItemSource } from "./style-source-control";
type UseSortable<Item> = {
items: Array<Item>;
diff --git a/apps/builder/app/builder/shared/tree/index.tsx b/apps/builder/app/builder/shared/tree/index.tsx
index fc45e25572..2fe35ceb4c 100644
--- a/apps/builder/app/builder/shared/tree/index.tsx
+++ b/apps/builder/app/builder/shared/tree/index.tsx
@@ -1,4 +1,4 @@
-import { useCallback } from "react";
+import { useCallback, useRef } from "react";
import { useStore } from "@nanostores/react";
import {
Tree,
@@ -32,6 +32,7 @@ import type { InstanceSelector } from "~/shared/tree-utils";
import { shallowEqual } from "shallow-equal";
import { EyeconClosedIcon, EyeconOpenIcon } from "@webstudio-is/icons";
import { nanoid } from "nanoid";
+import { mergeRefs } from "@react-aria/utils";
const ShowToggle = ({
show,
@@ -198,9 +199,8 @@ export const InstanceTree = (
);
}}
prefix={<MetaIcon icon={meta.icon} />}
- >
- {label}
- </TreeItem>
+ value={label}
+ />
</TreeItemBody>
);
},
@@ -231,7 +231,7 @@ export const InstanceTree = (
const TreeItem = ({
prefix,
- children,
+ value,
isEditing,
isEditable = false,
onChangeValue,
@@ -240,16 +240,18 @@ const TreeItem = ({
isEditable: boolean;
isEditing: boolean;
prefix?: React.ReactNode;
- children: React.ReactNode;
+ value: string;
onChangeValue: (value: string) => void;
onChangeEditing: (isEditing: boolean) => void;
}) => {
+ const editableRef = useRef<HTMLDivElement | null>(null);
const { ref, handlers } = useContentEditable({
+ value,
isEditable,
isEditing,
onChangeValue: (value: string) => {
onChangeValue(value);
- const button = ref.current?.closest(
+ const button = editableRef.current?.closest(
"[data-item-button-id]"
) as HTMLElement;
button?.focus();
@@ -259,12 +261,12 @@ const TreeItem = ({
return (
<EditableTreeItemLabel
- ref={ref}
+ ref={mergeRefs(editableRef, ref)}
{...handlers}
isEditing={isEditing}
prefix={prefix}
>
- {children}
+ {value}
</EditableTreeItemLabel>
);
};
diff --git a/apps/builder/app/shared/dom-hooks/use-content-editable.ts b/apps/builder/app/shared/dom-hooks/use-content-editable.ts
index 9de0878ab7..c926b82686 100644
--- a/apps/builder/app/shared/dom-hooks/use-content-editable.ts
+++ b/apps/builder/app/shared/dom-hooks/use-content-editable.ts
@@ -11,16 +11,28 @@ export const useContentEditable = ({
isEditing,
onChangeEditing,
onChangeValue,
+ value,
}: {
isEditable: boolean;
isEditing: boolean;
onChangeEditing: (isEditing: boolean) => void;
onChangeValue: (value: string) => void;
+ value: string;
}) => {
- const elementRef = useRef<HTMLDivElement>(null);
- const lastValueRef = useRef<string>("");
+ const elementRef = useRef<HTMLDivElement | null>(null);
+ const setTextContent = () => {
+ if (elementRef.current && isEditing === false) {
+ elementRef.current.textContent = value;
+ }
+ };
+ const ref = (element: HTMLDivElement | null) => {
+ elementRef.current = element;
+ setTextContent();
+ };
const getValue = () => elementRef.current?.textContent ?? "";
+ useEffect(setTextContent, [value, isEditing]);
+
useEffect(() => {
const element = elementRef.current;
if (element === null) {
@@ -39,7 +51,6 @@ export const useContentEditable = ({
requestAnimationFrame(() => {
element.focus();
getSelection()?.selectAllChildren(element);
- lastValueRef.current = getValue();
});
return;
}
@@ -54,25 +65,33 @@ export const useContentEditable = ({
}
}, [isEditing]);
- const handleFinishEditing = (
- event: KeyboardEvent<Element> | FocusEvent<Element>
- ) => {
+ const handleEnd = (event: KeyboardEvent<Element> | FocusEvent<Element>) => {
event.preventDefault();
if (isEditing) {
onChangeEditing(false);
}
- onChangeValue(getValue());
- lastValueRef.current = "";
+ };
+
+ const handleComplete = (
+ event: KeyboardEvent<Element> | FocusEvent<Element>
+ ) => {
+ event.preventDefault();
+ if (isEditing === false) {
+ return;
+ }
+ const nextValue = getValue();
+ handleEnd(event);
+ onChangeValue(nextValue);
};
const handleKeyDown: KeyboardEventHandler = (event) => {
if (event.key === "Enter") {
- handleFinishEditing(event);
+ handleComplete(event);
return;
}
if (event.key === "Escape" && elementRef.current !== null) {
- elementRef.current.textContent = lastValueRef.current;
- handleFinishEditing(event);
+ elementRef.current.textContent = value;
+ handleEnd(event);
}
};
@@ -84,9 +103,9 @@ export const useContentEditable = ({
const handlers = {
onKeyDown: handleKeyDown,
- onBlur: handleFinishEditing,
+ onBlur: handleComplete,
onDoubleClick: handleDoubleClick,
};
- return { ref: elementRef, handlers };
+ return { ref, handlers };
};
diff --git a/apps/builder/app/shared/share-project/share-project.tsx b/apps/builder/app/shared/share-project/share-project.tsx
index c9e665d271..6563a97740 100644
--- a/apps/builder/app/shared/share-project/share-project.tsx
+++ b/apps/builder/app/shared/share-project/share-project.tsx
@@ -17,7 +17,6 @@ import {
keyframes,
Text,
InputField,
- PopoverPortal,
Link,
buttonStyle,
IconButton,
@@ -119,164 +118,162 @@ const Menu = ({ name, hasProPlan, value, onChange, onDelete }: MenuProps) => {
aria-label="Menu Button for options"
></Button>
</PopoverTrigger>
- <PopoverPortal>
- <PopoverContent
- css={{
- padding: 0,
- width: theme.spacing[24],
- }}
- sideOffset={0}
- onInteractOutside={saveCustomLinkName}
- >
- <Item>
- <Label htmlFor={ids.name}>Name</Label>
- <InputField
- id={ids.name}
- color={customLinkName.length === 0 ? "error" : undefined}
- value={customLinkName}
- onChange={(event) => setCustomLinkName(event.target.value)}
- onKeyDown={(event) => {
- if (event.key === "Enter") {
- saveCustomLinkName();
- setIsOpen(false);
- }
- }}
- onBlur={saveCustomLinkName}
- placeholder="Share Project"
- name="Name"
- autoFocus
- />
- </Item>
- <Separator />
- <Item>
- <Label>Permissions</Label>
- <Permission
- checked={value.relation === "viewers"}
- onCheckedChange={handleCheckedChange("viewers")}
- title="View"
- //info="Recipients can view, copy instances and clone the project"
- info={
- <Flex direction="column">
- Recipients can view, copy instances and clone the project.
- {hasProPlan !== true && (
- <>
- <br />
- <br />
- Upgrade to a Pro account to set additional permissions.
- <br /> <br />
- <Link
- className={buttonStyle({ color: "gradient" })}
- color="contrast"
- underline="none"
- href="https://webstudio.is/pricing"
- target="_blank"
- >
- Upgrade
- </Link>
- </>
- )}
- </Flex>
+ <PopoverContent
+ css={{
+ //padding: 0,
+ width: theme.spacing[24],
+ }}
+ sideOffset={0}
+ onInteractOutside={saveCustomLinkName}
+ >
+ <Item>
+ <Label htmlFor={ids.name}>Name</Label>
+ <InputField
+ id={ids.name}
+ color={customLinkName.length === 0 ? "error" : undefined}
+ value={customLinkName}
+ onChange={(event) => setCustomLinkName(event.target.value)}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") {
+ saveCustomLinkName();
+ setIsOpen(false);
}
- />
+ }}
+ onBlur={saveCustomLinkName}
+ placeholder="Share Project"
+ name="Name"
+ autoFocus
+ />
+ </Item>
+ <Separator />
+ <Item>
+ <Label>Permissions</Label>
+ <Permission
+ checked={value.relation === "viewers"}
+ onCheckedChange={handleCheckedChange("viewers")}
+ title="View"
+ //info="Recipients can view, copy instances and clone the project"
+ info={
+ <Flex direction="column">
+ Recipients can view, copy instances and clone the project.
+ {hasProPlan !== true && (
+ <>
+ <br />
+ <br />
+ Upgrade to a Pro account to set additional permissions.
+ <br /> <br />
+ <Link
+ className={buttonStyle({ color: "gradient" })}
+ color="contrast"
+ underline="none"
+ href="https://webstudio.is/pricing"
+ target="_blank"
+ >
+ Upgrade
+ </Link>
+ </>
+ )}
+ </Flex>
+ }
+ />
+ <Grid
+ css={{
+ ml: theme.spacing[6],
+ }}
+ >
<Grid
+ gap={1}
+ flow={"column"}
css={{
- ml: theme.spacing[6],
+ alignItems: "center",
+ justifyContent: "start",
}}
>
- <Grid
- gap={1}
- flow={"column"}
- css={{
- alignItems: "center",
- justifyContent: "start",
+ <Checkbox
+ disabled={hasProPlan !== true}
+ checked={value.canClone}
+ onCheckedChange={(canClone) => {
+ onChange({ ...value, canClone: Boolean(canClone) });
}}
- >
- <Checkbox
- disabled={hasProPlan !== true}
- checked={value.canClone}
- onCheckedChange={(canClone) => {
- onChange({ ...value, canClone: Boolean(canClone) });
- }}
- id={ids.canClone}
- />
- <Label htmlFor={ids.canClone} disabled={hasProPlan !== true}>
- Can clone
- </Label>
- </Grid>
- <Grid
- gap={1}
- flow={"column"}
- css={{
- alignItems: "center",
- justifyContent: "start",
+ id={ids.canClone}
+ />
+ <Label htmlFor={ids.canClone} disabled={hasProPlan !== true}>
+ Can clone
+ </Label>
+ </Grid>
+ <Grid
+ gap={1}
+ flow={"column"}
+ css={{
+ alignItems: "center",
+ justifyContent: "start",
+ }}
+ >
+ <Checkbox
+ disabled={hasProPlan !== true}
+ checked={value.canCopy}
+ onCheckedChange={(canCopy) => {
+ onChange({ ...value, canCopy: Boolean(canCopy) });
}}
- >
- <Checkbox
- disabled={hasProPlan !== true}
- checked={value.canCopy}
- onCheckedChange={(canCopy) => {
- onChange({ ...value, canCopy: Boolean(canCopy) });
- }}
- id={ids.canCopy}
- />
- <Label htmlFor={ids.canCopy} disabled={hasProPlan !== true}>
- Can copy
- </Label>
- </Grid>
+ id={ids.canCopy}
+ />
+ <Label htmlFor={ids.canCopy} disabled={hasProPlan !== true}>
+ Can copy
+ </Label>
</Grid>
+ </Grid>
- <Permission
- onCheckedChange={handleCheckedChange("builders")}
- checked={value.relation === "builders"}
- title="Build"
- info="Recipients can make any changes but can not publish the project."
- />
+ <Permission
+ onCheckedChange={handleCheckedChange("builders")}
+ checked={value.relation === "builders"}
+ title="Build"
+ info="Recipients can make any changes but can not publish the project."
+ />
- <Permission
- disabled={hasProPlan !== true}
- onCheckedChange={handleCheckedChange("administrators")}
- checked={value.relation === "administrators"}
- title="Admin"
- info={
- <Flex direction="column">
- Recipients can make any changes and can also publish the
- project.
- {hasProPlan !== true && (
- <>
- <br />
- <br />
- Upgrade to a Pro account to share with Admin permissions.
- <br /> <br />
- <Link
- className={buttonStyle({ color: "gradient" })}
- color="contrast"
- underline="none"
- href="https://webstudio.is/pricing"
- target="_blank"
- >
- Upgrade
- </Link>
- </>
- )}
- </Flex>
- }
- />
- </Item>
- <Separator />
- <Item>
- {/* @todo need a menu item that looks like one from dropdown but without DropdownMenu */}
- <Button
- color="neutral-destructive"
- onClick={() => {
- onDelete();
- }}
- >
- Delete Link
- </Button>
- </Item>
- </PopoverContent>
- </PopoverPortal>
+ <Permission
+ disabled={hasProPlan !== true}
+ onCheckedChange={handleCheckedChange("administrators")}
+ checked={value.relation === "administrators"}
+ title="Admin"
+ info={
+ <Flex direction="column">
+ Recipients can make any changes and can also publish the
+ project.
+ {hasProPlan !== true && (
+ <>
+ <br />
+ <br />
+ Upgrade to a Pro account to share with Admin permissions.
+ <br /> <br />
+ <Link
+ className={buttonStyle({ color: "gradient" })}
+ color="contrast"
+ underline="none"
+ href="https://webstudio.is/pricing"
+ target="_blank"
+ >
+ Upgrade
+ </Link>
+ </>
+ )}
+ </Flex>
+ }
+ />
+ </Item>
+ <Separator />
+ <Item>
+ {/* @todo need a menu item that looks like one from dropdown but without DropdownMenu */}
+ <Button
+ color="neutral-destructive"
+ onClick={() => {
+ onDelete();
+ }}
+ >
+ Delete Link
+ </Button>
+ </Item>
+ </PopoverContent>
</Popover>
);
};
diff --git a/packages/design-system/src/components/button.stories.tsx b/packages/design-system/src/components/button.stories.tsx
index 71e6396644..616fc61079 100644
--- a/packages/design-system/src/components/button.stories.tsx
+++ b/packages/design-system/src/components/button.stories.tsx
@@ -1,14 +1,10 @@
import type { ComponentProps } from "react";
import { EllipsesIcon, CrossIcon, TrashIcon } from "@webstudio-is/icons";
import { Button as ButtonComponent } from "./button";
-import {
- Popover,
- PopoverContent,
- PopoverTrigger,
- PopoverContentContainer,
-} from "./popover";
+import { Popover, PopoverContent, PopoverTrigger } from "./popover";
import { Text } from "./text";
import { StorySection, StoryGrid } from "./storybook";
+import { theme } from "../stitches.config";
export default {
title: "Library/Button",
@@ -119,10 +115,8 @@ export const Button = ({
<PopoverTrigger asChild>
<ButtonComponent prefix={<TrashIcon />}>Open</ButtonComponent>
</PopoverTrigger>
- <PopoverContent>
- <PopoverContentContainer>
- <Text>Some content</Text>
- </PopoverContentContainer>
+ <PopoverContent css={{ padding: theme.spacing[7] }}>
+ <Text>Some content</Text>
</PopoverContent>
</Popover>
</StorySection>
diff --git a/packages/design-system/src/components/popover.stories.tsx b/packages/design-system/src/components/popover.stories.tsx
index d4a6cfe1ef..719364f147 100644
--- a/packages/design-system/src/components/popover.stories.tsx
+++ b/packages/design-system/src/components/popover.stories.tsx
@@ -5,11 +5,11 @@ import {
PopoverClose,
PopoverContent,
PopoverTrigger,
- PopoverContentContainer,
- PopoverMenuItemContainer,
PopoverSeparator,
} from "./popover";
import { MenuItemButton } from "./menu";
+import { theme } from "../stitches.config";
+import { Flex } from "./flex";
export default {
title: "Library/Popover",
@@ -21,15 +21,15 @@ const PopoverDemo = () => (
<Button>Open</Button>
</PopoverTrigger>
<PopoverContent>
- <PopoverContentContainer>
+ <Flex css={{ padding: theme.spacing[7] }}>
<Text>Some content</Text>
- </PopoverContentContainer>
+ </Flex>
<PopoverSeparator />
- <PopoverMenuItemContainer>
+ <Flex css={{ padding: theme.spacing[3] }}>
<PopoverClose asChild>
<MenuItemButton>Close</MenuItemButton>
</PopoverClose>
- </PopoverMenuItemContainer>
+ </Flex>
</PopoverContent>
</Popover>
);
diff --git a/packages/design-system/src/components/popover.tsx b/packages/design-system/src/components/popover.tsx
index b98fa567df..47798726d4 100644
--- a/packages/design-system/src/components/popover.tsx
+++ b/packages/design-system/src/components/popover.tsx
@@ -12,7 +12,6 @@ const contentStyle = css({
boxShadow: `${theme.shadows.menuDropShadow}, inset 0 0 0 1px ${theme.colors.borderMenuInner}`,
background: theme.colors.backgroundMenu,
borderRadius: theme.borderRadius[6],
- padding: `${theme.spacing[5]} 0`,
display: "flex",
flexDirection: "column",
maxWidth: "max-content",
@@ -70,20 +69,9 @@ PopoverContent.displayName = "PopoverContent";
export const PopoverTrigger = Primitive.Trigger;
export const PopoverClose = Primitive.Close;
-export const PopoverContentContainer = styled("div", {
- margin: `0 ${theme.spacing[7]}`,
-});
-
-export const PopoverMenuItemContainer = styled("div", {
- display: "flex",
- margin: `0 ${theme.spacing[3]}`,
-});
-
export const PopoverMenuItemRightSlot = styled("span", {
marginLeft: "auto",
display: "flex",
});
-export const PopoverSeparator = styled(Separator, {
- margin: `${theme.spacing[5]} 0`,
-});
+export const PopoverSeparator = styled(Separator);
diff --git a/packages/design-system/src/stitches.config.ts b/packages/design-system/src/stitches.config.ts
index 08ebb5811d..ac27cc1810 100644
--- a/packages/design-system/src/stitches.config.ts
+++ b/packages/design-system/src/stitches.config.ts
@@ -13,45 +13,6 @@ import type * as Stitches from "@stitches/react";
export type { VariantProps } from "@stitches/react";
import * as figma from "./__generated__/figma-design-tokens";
-const spacing = {
- 0: "0px",
- 1: "1px",
- 2: "2px",
- 3: "4px",
- 4: "6px",
- 5: "8px",
- 6: "10px",
- 7: "12px",
- 8: "14px",
- 9: "16px",
- 10: "20px",
- 11: "24px",
- 12: "28px",
- 13: "32px",
- 14: "36px",
- 15: "40px",
- 16: "44px",
- 17: "48px",
- 18: "56px",
- 19: "64px",
- 20: "80px",
- 21: "96px",
- 22: "112px",
- 23: "128px",
- 24: "144px",
- 25: "160px",
- 26: "176px",
- 27: "192px",
- 28: "208px",
- 29: "224px",
- 30: "240px",
- 31: "256px",
- 32: "288px",
- 33: "320px",
- 34: "384px",
- 35: "448px",
-} as const;
-
const { styled, css, getCssText, globalCss, keyframes, config, reset } =
createStitches({
theme: {
@@ -110,7 +71,44 @@ const { styled, css, getCssText, globalCss, keyframes, config, reset } =
opacity: {
1: "0.4",
},
- spacing,
+ spacing: {
+ 0: "0px",
+ 1: "1px",
+ 2: "2px",
+ 3: "4px",
+ 4: "6px",
+ 5: "8px",
+ 6: "10px",
+ 7: "12px",
+ 8: "14px",
+ 9: "16px",
+ 10: "20px",
+ 11: "24px",
+ 12: "28px",
+ 13: "32px",
+ 14: "36px",
+ 15: "40px",
+ 16: "44px",
+ 17: "48px",
+ 18: "56px",
+ 19: "64px",
+ 20: "80px",
+ 21: "96px",
+ 22: "112px",
+ 23: "128px",
+ 24: "144px",
+ 25: "160px",
+ 26: "176px",
+ 27: "192px",
+ 28: "208px",
+ 29: "224px",
+ 30: "240px",
+ 31: "256px",
+ 32: "288px",
+ 33: "320px",
+ 34: "384px",
+ 35: "448px",
+ },
/**
* Use instead: textVariants / textStyles / <Text />