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.

Add `data-state="open"` support to Button (#950)

Roman Pominov528c3acdb525
diff --git a/.lostpixel/baseline/library-button--button.png b/.lostpixel/baseline/library-button--button.png
index e6ad6007e6..341e1cb751 100644
Binary files a/.lostpixel/baseline/library-button--button.png and b/.lostpixel/baseline/library-button--button.png differ
diff --git a/.lostpixel/baseline/library-floating-panel-dialog--dialog.png b/.lostpixel/baseline/library-floating-panel-dialog--dialog.png
index f993952962..e7f2988e40 100644
Binary files a/.lostpixel/baseline/library-floating-panel-dialog--dialog.png and b/.lostpixel/baseline/library-floating-panel-dialog--dialog.png differ
diff --git a/.lostpixel/baseline/library-floating-panel-popover--popover.png b/.lostpixel/baseline/library-floating-panel-popover--popover.png
index 8bd632c05b..8db5344ea9 100644
Binary files a/.lostpixel/baseline/library-floating-panel-popover--popover.png and b/.lostpixel/baseline/library-floating-panel-popover--popover.png differ
diff --git a/.lostpixel/baseline/library-menu-menu-item--menu-menu-item.png b/.lostpixel/baseline/library-menu-menu-item--menu-menu-item.png
index 8f45399a20..ca7bf0d468 100644
Binary files a/.lostpixel/baseline/library-menu-menu-item--menu-menu-item.png and b/.lostpixel/baseline/library-menu-menu-item--menu-menu-item.png differ
diff --git a/.lostpixel/baseline/library-popover--popover.png b/.lostpixel/baseline/library-popover--popover.png
index 911b3798c0..bfc81e5a47 100644
Binary files a/.lostpixel/baseline/library-popover--popover.png and b/.lostpixel/baseline/library-popover--popover.png differ
diff --git a/packages/design-system/src/components/button.stories.tsx b/packages/design-system/src/components/button.stories.tsx
index 646b2dab78..236dfdcd4c 100644
--- a/packages/design-system/src/components/button.stories.tsx
+++ b/packages/design-system/src/components/button.stories.tsx
@@ -1,6 +1,13 @@
import { type ComponentProps } from "react";
import { MenuIcon, CrossIcon, TrashIcon } from "@webstudio-is/icons";
import { Button as ButtonComponent } from "./button";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+ PopoverContentContainer,
+} from "./popover";
+import { Text } from "./text";
import { StorySection, StoryGrid } from "./storybook";
export default {
@@ -77,6 +84,19 @@ export const Button = ({
<ButtonComponent prefix={<TrashIcon />} />
</StoryGrid>
</StorySection>
+
+ <StorySection title="Used as a Trigger for something that opens">
+ <Popover defaultOpen>
+ <PopoverTrigger asChild>
+ <ButtonComponent prefix={<TrashIcon />}>Open</ButtonComponent>
+ </PopoverTrigger>
+ <PopoverContent>
+ <PopoverContentContainer>
+ <Text>Some content</Text>
+ </PopoverContentContainer>
+ </PopoverContent>
+ </Popover>
+ </StorySection>
</>
);
diff --git a/packages/design-system/src/components/button.tsx b/packages/design-system/src/components/button.tsx
index af4fff985b..8c502fea2c 100644
--- a/packages/design-system/src/components/button.tsx
+++ b/packages/design-system/src/components/button.tsx
@@ -51,31 +51,31 @@ const perColorStyle = (variant: ButtonColor) => ({
background: variant === "ghost" ? "transparent" : backgrounds[variant],
color: foregrounds[variant],
- "&[data-button-state=auto]:hover, &[data-button-state=hover]": {
+ "&[data-state=auto]:hover, &[data-state=hover]": {
background: backgroundColors(
backgrounds[variant],
theme.colors.backgroundButtonHover
),
},
- "&[data-button-state=auto]:focus-visible, &[data-button-state=focus]": {
+ "&[data-state=auto]:focus-visible, &[data-state=focus]": {
outline: `2px solid ${theme.colors.borderFocus}`,
outlineOffset: "1px",
},
- "&[data-button-state=auto]:active, &[data-button-state=pressed]": {
+ "&[data-state=auto]:active, &[data-state=pressed]": {
background: backgroundColors(
backgrounds[variant],
theme.colors.backgroundButtonPressed
),
},
- "&[data-button-state=disabled]": {
+ "&[data-state=disabled]": {
background: theme.colors.backgroundButtonDisabled,
color: theme.colors.foregroundDisabled,
},
- "&[data-button-state=pending]": {
+ "&[data-state=pending]": {
cursor: "wait",
},
});
@@ -125,25 +125,43 @@ type ButtonProps = {
// this is a replacement for icon/icon-left/icon-right in Figma
prefix?: ReactNode;
suffix?: ReactNode;
+
+ // might be set when <Button> is asChild
+ "data-state"?: string;
} & Omit<ComponentProps<"button">, "prefix">;
export const Button = forwardRef(
(
{
disabled,
- state = "auto",
+ state,
prefix,
suffix,
children,
+ "data-state": dataState,
...restProps
}: ButtonProps,
ref: Ref<HTMLButtonElement>
) => {
+ // when button is used as a trigger for something that opens
+ // <SomeTrigger asChild><Button /></SomeTrigger>
+ let finalState = dataState === "open" ? "pressed" : undefined;
+
+ // "state" wins over "data-state"
+ if (state !== undefined) {
+ finalState = state;
+ }
+
+ // "disabled" wins over everything
+ if (disabled) {
+ finalState = "disabled";
+ }
+
return (
<StyledButton
{...restProps}
disabled={disabled || state === "pending"}
- data-button-state={disabled ? "disabled" : state}
+ data-state={finalState ?? "auto"}
ref={ref}
>
{prefix}