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.

Redesign text toolbar (#567)

Bogdan Chadkin7637d486a734
diff --git a/.lostpixel/baseline/components-icon-button--default.png b/.lostpixel/baseline/components-icon-button--default.png
new file mode 100644
index 0000000000..ae2f42a838
Binary files /dev/null and b/.lostpixel/baseline/components-icon-button--default.png differ
diff --git a/apps/designer/app/canvas/features/text-editor/toolbar-connector.tsx b/apps/designer/app/canvas/features/text-editor/toolbar-connector.tsx
index a94ae0033a..fdef3a191b 100644
--- a/apps/designer/app/canvas/features/text-editor/toolbar-connector.tsx
+++ b/apps/designer/app/canvas/features/text-editor/toolbar-connector.tsx
@@ -167,6 +167,16 @@ export const ToolbarConnectorPlugin = () => {
// dispatch commands sent from toolbar
useSubscribe("formatTextToolbar", (type) => {
+ let isSuperscript = false;
+ let isSubscript = false;
+ editor.getEditorState().read(() => {
+ const selection = $getSelection();
+ if ($isRangeSelection(selection)) {
+ isSuperscript = selection.hasFormat("superscript");
+ isSubscript = selection.hasFormat("subscript");
+ }
+ });
+
if (type === "bold") {
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "bold");
}
@@ -175,9 +185,17 @@ export const ToolbarConnectorPlugin = () => {
}
if (type === "superscript") {
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "superscript");
+ // remove subscript if superscript is added
+ if (isSubscript) {
+ editor.dispatchCommand(FORMAT_TEXT_COMMAND, "subscript");
+ }
}
if (type === "subscript") {
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "subscript");
+ // remove superscript if subscript is added
+ if (isSuperscript) {
+ editor.dispatchCommand(FORMAT_TEXT_COMMAND, "superscript");
+ }
}
if (type === "link") {
const editorState = editor.getEditorState();
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx b/apps/designer/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
index a2e59c9c5f..c80def0ca4 100644
--- a/apps/designer/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
@@ -1,4 +1,4 @@
-import { useRef, useEffect, type MouseEventHandler } from "react";
+import { useRef, useEffect } from "react";
import { computePosition, flip, offset, shift } from "@floating-ui/dom";
import { type Publish } from "~/shared/pubsub";
import {
@@ -6,7 +6,7 @@ import {
type TextToolbarState,
useTextToolbarState,
} from "~/designer/shared/nano-states";
-import { ToggleGroupRoot, ToggleGroupItem } from "@webstudio-is/design-system";
+import { Flex, IconButton, Tooltip } from "@webstudio-is/design-system";
import {
FontBoldIcon,
FontItalicIcon,
@@ -14,7 +14,7 @@ import {
SubscriptIcon,
Link2Icon,
BrushIcon,
- FormatClearIcon,
+ CrossSmallIcon,
} from "@webstudio-is/icons";
import { useSubscribe } from "~/shared/pubsub";
@@ -41,11 +41,6 @@ export const useSubscribeTextToolbar = () => {
useSubscribe("hideTextToolbar", () => setTextToolbar(undefined));
};
-const onClickPreventDefault: MouseEventHandler<HTMLDivElement> = (event) => {
- event.preventDefault();
- event.stopPropagation();
-};
-
const getRectForRelativeRect = (parent: DOMRect, rel: DOMRect) => {
return {
x: parent.x + rel.x,
@@ -87,86 +82,104 @@ const Toolbar = ({ state, onToggle }: ToolbarProps) => {
}
}, [state.selectionRect]);
- const value: Format[] = [];
- if (state.isBold) {
- value.push("bold");
- }
- if (state.isItalic) {
- value.push("italic");
- }
- if (state.isSuperscript) {
- value.push("superscript");
- }
- if (state.isSubscript) {
- value.push("subscript");
- }
- if (state.isLink) {
- value.push("link");
- }
- if (state.isSpan) {
- value.push("span");
- }
+ const isCleared =
+ state.isBold === false &&
+ state.isItalic === false &&
+ state.isSuperscript === false &&
+ state.isSubscript === false &&
+ state.isLink === false &&
+ state.isSpan === false;
return (
- <ToggleGroupRoot
+ <Flex
ref={rootRef}
- type="multiple"
- value={value}
- onValueChange={(newValues: Format[]) => {
- // @todo refactor with per button callback
- if (state.isBold !== newValues.includes("bold")) {
- onToggle("bold");
- }
- if (state.isItalic !== newValues.includes("italic")) {
- onToggle("italic");
- }
- if (state.isSuperscript !== newValues.includes("superscript")) {
- onToggle("superscript");
- }
- if (state.isSubscript !== newValues.includes("subscript")) {
- onToggle("subscript");
- }
- if (state.isLink !== newValues.includes("link")) {
- onToggle("link");
- }
- if (state.isSpan !== newValues.includes("span")) {
- onToggle("span");
- }
- if (newValues.includes("clear")) {
- onToggle("clear");
- }
- }}
- onClick={onClickPreventDefault}
+ gap={2}
css={{
position: "absolute",
top: 0,
left: 0,
pointerEvents: "auto",
background: "$loContrast",
+ padding: "$spacing$3",
+ borderRadius: "$borderRadius$6",
+ border: "1px solid $slate8",
+ filter:
+ "drop-shadow(0px 2px 7px rgba(0, 0, 0, 0.1)) drop-shadow(0px 5px 17px rgba(0, 0, 0, 0.15))",
+ }}
+ onClick={(event) => {
+ event.stopPropagation();
}}
>
- <ToggleGroupItem value="bold">
- <FontBoldIcon />
- </ToggleGroupItem>
- <ToggleGroupItem value="italic">
- <FontItalicIcon />
- </ToggleGroupItem>
- <ToggleGroupItem value="superscript">
- <SuperscriptIcon />
- </ToggleGroupItem>
- <ToggleGroupItem value="subscript">
- <SubscriptIcon />
- </ToggleGroupItem>
- <ToggleGroupItem value="link">
- <Link2Icon />
- </ToggleGroupItem>
- <ToggleGroupItem value="span">
- <BrushIcon />
- </ToggleGroupItem>
- <ToggleGroupItem value="clear">
- <FormatClearIcon />
- </ToggleGroupItem>
- </ToggleGroupRoot>
+ <Tooltip content="Clear styles">
+ <IconButton
+ aria-label="Clear styles"
+ disabled={isCleared}
+ onClick={() => onToggle("clear")}
+ >
+ <CrossSmallIcon />
+ </IconButton>
+ </Tooltip>
+
+ <Tooltip content="Bold">
+ <IconButton
+ aria-label="Bold"
+ variant={state.isBold ? "set" : "default"}
+ onClick={() => onToggle("bold")}
+ >
+ <FontBoldIcon />
+ </IconButton>
+ </Tooltip>
+
+ <Tooltip content="Italic">
+ <IconButton
+ aria-label="Italic"
+ variant={state.isItalic ? "set" : "default"}
+ onClick={() => onToggle("italic")}
+ >
+ <FontItalicIcon />
+ </IconButton>
+ </Tooltip>
+
+ <Tooltip content="Superscript">
+ <IconButton
+ aria-label="Superscript"
+ variant={state.isSuperscript ? "set" : "default"}
+ onClick={() => onToggle("superscript")}
+ >
+ <SuperscriptIcon />
+ </IconButton>
+ </Tooltip>
+
+ <Tooltip content="Subscript">
+ <IconButton
+ aria-label="Subscript"
+ variant={state.isSubscript ? "set" : "default"}
+ onClick={() => onToggle("subscript")}
+ >
+ <SubscriptIcon />
+ </IconButton>
+ </Tooltip>
+
+ <Tooltip content="Inline link">
+ <IconButton
+ aria-label="Inline link"
+ variant={state.isLink ? "set" : "default"}
+ onClick={() => onToggle("link")}
+ >
+ <Link2Icon />
+ </IconButton>
+ </Tooltip>
+
+ <Tooltip content="Wrap with span">
+ <IconButton
+ aria-label="Wrap with span"
+ variant={state.isSpan ? "set" : "default"}
+ onClick={() => onToggle("span")}
+ >
+ <BrushIcon />
+ </IconButton>
+ </Tooltip>
+ </Flex>
);
};
diff --git a/packages/design-system/src/components/icon-button.stories.tsx b/packages/design-system/src/components/icon-button.stories.tsx
new file mode 100644
index 0000000000..17adf6ac5b
--- /dev/null
+++ b/packages/design-system/src/components/icon-button.stories.tsx
@@ -0,0 +1,46 @@
+import type { ComponentStory } from "@storybook/react";
+import { CrossIcon } from "@webstudio-is/icons";
+import { Flex } from "./flex";
+import { IconButton } from "./icon-button";
+
+export default {
+ component: IconButton,
+};
+
+export const Default: ComponentStory<typeof IconButton> = () => {
+ return (
+ <Flex direction="column" gap={2}>
+ <Flex gap={2}>
+ <IconButton>
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ <IconButton variant="preset">
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ <IconButton variant="set">
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ <IconButton variant="inherited">
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ <IconButton variant="active">
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ </Flex>
+ <Flex gap={2}>
+ <IconButton disabled={true}>
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ <IconButton variant="preset" disabled={true}>
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ <IconButton variant="set" disabled={true}>
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ <IconButton variant="inherited" disabled={true}>
+ <CrossIcon fill="currentColor" />
+ </IconButton>
+ </Flex>
+ </Flex>
+ );
+};
diff --git a/packages/design-system/src/components/icon-button.tsx b/packages/design-system/src/components/icon-button.tsx
new file mode 100644
index 0000000000..0fc5826660
--- /dev/null
+++ b/packages/design-system/src/components/icon-button.tsx
@@ -0,0 +1,86 @@
+import { styled } from "../stitches.config";
+
+export const IconButton = styled("button", {
+ // reset styles
+ boxSizing: "border-box",
+ padding: 0,
+ appearance: "none",
+ backgroundColor: "transparent",
+ border: "1px solid transparent",
+ // center icon
+ display: "flex",
+ justifyContent: "center",
+ alignItems: "center",
+ // prevent shrinking inside flex box
+ flexShrink: 0,
+ // set size and shape
+ width: 28,
+ height: 28,
+ borderRadius: "$borderRadius$3",
+
+ "&:focus-visible": {
+ outline: "2px solid $blue10",
+ },
+ "&:disabled": {
+ borderColor: "transparent",
+ pointerEvents: "none",
+ },
+
+ variants: {
+ variant: {
+ default: {
+ color: "$slate12",
+ "&:hover": {
+ backgroundColor: "$slate6",
+ },
+ "&:disabled": {
+ color: "$slate8",
+ },
+ },
+ preset: {
+ backgroundColor: "$slate6",
+ borderColor: "$slate8",
+ color: "$slate12",
+ "&:hover": {
+ backgroundColor: "$slate8",
+ },
+ "&:disabled": {
+ color: "$slate8",
+ },
+ },
+ set: {
+ backgroundColor: "$blue4",
+ borderColor: "$blue6",
+ color: "$blue11",
+ "&:hover": {
+ backgroundColor: "$blue6",
+ },
+ "&:disabled": {
+ color: "$blue6",
+ },
+ },
+ inherited: {
+ backgroundColor: "$orange4",
+ borderColor: "$orange6",
+ color: "$orange11",
+ "&:hover": {
+ backgroundColor: "$orange6",
+ },
+ "&:disabled": {
+ color: "$orange6",
+ },
+ },
+ active: {
+ backgroundColor: "$blue10",
+ color: "White",
+ // non-interactive state because usually covered with overlay
+ },
+ },
+ },
+
+ defaultVariants: {
+ variant: "default",
+ },
+});
+
+IconButton.displayName = "IconButton";
diff --git a/packages/design-system/src/index.ts b/packages/design-system/src/index.ts
index 560728adee..4a8e3263a0 100644
--- a/packages/design-system/src/index.ts
+++ b/packages/design-system/src/index.ts
@@ -29,6 +29,7 @@ export { ScrollArea } from "./components/scrollbar";
export { Tooltip, InputErrorsTooltip } from "./components/tooltip";
export { Button } from "./components/button";
export { IconButtonDeprecated } from "./components/icon-button-deprecated";
+export { IconButton } from "./components/icon-button";
export { Box } from "./components/box";
export { ProgressBar } from "./components/progress-bar";
export * from "./components/popover";
diff --git a/packages/icons/icons/format-clear.svg b/packages/icons/icons/format-clear.svg
deleted file mode 100644
index c93169fe93..0000000000
--- a/packages/icons/icons/format-clear.svg
+++ /dev/null
@@ -1 +0,0 @@
-<svg xmlns="http://www.w3.org/2000/svg" height="48" width="48"><path d="M25.35 21.8 21.5 18l1.2-2.8h-3.95l-5.2-5.2H40v5H28.25ZM40.3 45.2 22.85 27.7 18.45 38H13l6-14.1L2.8 7.7l2.1-2.1 37.5 37.5Z"/></svg>
\ No newline at end of file
diff --git a/packages/icons/src/__generated__/format-clear.tsx b/packages/icons/src/__generated__/format-clear.tsx
deleted file mode 100644
index a7135ad685..0000000000
--- a/packages/icons/src/__generated__/format-clear.tsx
+++ /dev/null
@@ -1,15 +0,0 @@
-// Generated from icons/format-clear.svg
-
-import { forwardRef } from "react";
-import { IconProps } from "../types";
-
-// prettier-ignore
-export const FormatClearIcon = forwardRef<SVGSVGElement, IconProps>(
- ({ color = "currentColor", ...props }, forwardedRef) => {
- return (
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="16" height="16" fill={color} {...props} ref={forwardedRef}><path d="M25.35 21.8 21.5 18l1.2-2.8h-3.95l-5.2-5.2H40v5H28.25ZM40.3 45.2 22.85 27.7 18.45 38H13l6-14.1L2.8 7.7l2.1-2.1 37.5 37.5Z" /></svg>
- );
- }
-);
-
-FormatClearIcon.displayName = "FormatClearIcon";
diff --git a/packages/icons/src/__generated__/index.ts b/packages/icons/src/__generated__/index.ts
index 8b393b57a5..b9cfbad7cc 100644
--- a/packages/icons/src/__generated__/index.ts
+++ b/packages/icons/src/__generated__/index.ts
@@ -1,3 +1,2 @@
-export * from "./format-clear";
export * from "./subscript";
export * from "./superscript";