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
Refactor controls and add stories
Andrey Okonetchnikova7b7da0815f0
diff --git a/app/designer/features/props-panel/control.stories.tsx b/app/designer/features/props-panel/control.stories.tsxnew file mode 100644index 0000000000..046ddd910b--- /dev/null+++ b/app/designer/features/props-panel/control.stories.tsx@@ -0,0 +1,19 @@+import { ComponentStoryObj } from "@storybook/react";+import { Control } from "./control";++export default {+ component: Control,+};++export const Primitive: ComponentStoryObj<typeof Control> = {+ args: {+ type: "text",+ },+};++export const Options: ComponentStoryObj<typeof Control> = {+ args: {+ type: "radio",+ options: ["option1", "option2"],+ },+};diff --git a/app/designer/features/props-panel/control.tsx b/app/designer/features/props-panel/control.tsxnew file mode 100644index 0000000000..d0f11e4c40--- /dev/null+++ b/app/designer/features/props-panel/control.tsx@@ -0,0 +1,185 @@+import { UserProp } from "@webstudio-is/sdk";+import React, { ComponentProps } from "react";+import {+ Flex,+ Label,+ Radio,+ RadioGroup,+ Select,+ Switch,+ TextField,+} from "~/shared/design-system";+import { Checkbox } from "~/shared/design-system/components/checkbox";++type BaseControlProps = {+ value?: UserProp["value"];+ defaultValue?: UserProp["value"];+ onChange: (value: UserProp["value"]) => void;+};+type TextControlProps = BaseControlProps & {+ type?: ComponentProps<typeof TextField>["type"];+ defaultValue?: UserProp["value"];+};+const TextControl = ({+ value,+ defaultValue,+ type,+ onChange,+}: TextControlProps) => (+ <TextField+ type={type}+ variant="ghost"+ placeholder="Value"+ name="value"+ value={String(value || defaultValue || "")}+ onChange={(event) => {+ onChange(event.target.value);+ }}+ />+);+type CheckboxControlProps = BaseControlProps & {+ options: Array<string>;+};+const CheckboxControl = ({+ value,+ options,+ defaultValue,+ onChange,+}: CheckboxControlProps) => (+ <RadioGroup+ css={{ flexDirection: "column" }}+ name="value"+ value={String(value || defaultValue || "")}+ onValueChange={onChange}+ >+ {options.map((value) => (+ <Flex align="center" gap="1" key={value}>+ <Checkbox value={value} />+ <Label>{value}</Label>+ </Flex>+ ))}+ </RadioGroup>+);+type RadioControlProps = BaseControlProps & {+ options: Array<string>;+};+const RadioControl = ({+ value,+ options,+ defaultValue,+ onChange,+}: RadioControlProps) => (+ <RadioGroup+ css={{ flexDirection: "column" }}+ name="value"+ value={String(value || defaultValue || "")}+ onValueChange={onChange}+ >+ {options.map((value) => (+ <Flex align="center" gap="1" key={value}>+ <Radio value={value} />+ <Label>{value}</Label>+ </Flex>+ ))}+ </RadioGroup>+);+type SelectControlProps = BaseControlProps & {+ options: Array<string>;+};+const SelectControl = ({+ value,+ options,+ defaultValue,+ onChange,+}: SelectControlProps) => (+ <Select+ name="value"+ value={String(value || defaultValue || "")}+ onChange={(event) => {+ onChange(event.target.value);+ }}+ >+ {options.map((optionValue) => (+ <option value={optionValue} key={optionValue}>+ {optionValue}+ </option>+ ))}+ </Select>+);++const BooleanControl = ({+ value,+ defaultValue,+ onChange,+}: BaseControlProps) => (+ <Switch+ name="value"+ defaultChecked={Boolean(defaultValue)}+ checked={value === true}+ onCheckedChange={onChange}+ />+);++const NotImplemented = () => <div />;++type SimpleControlProps = BaseControlProps & {+ type:+ | "boolean"+ | "array"+ | "color"+ | "date"+ | "number"+ | "range"+ | "object"+ | "text";+};+type OptionsControlProps = BaseControlProps & {+ type:+ | "radio"+ | "inline-radio"+ | "check"+ | "inline-check"+ | "select"+ | "multi-select";+ options: Array<string>;+};++export type ControlProps = SimpleControlProps | OptionsControlProps;++// eslint-disable-next-line func-style+export function Control(props: ControlProps) {+ switch (props.type) {+ case "boolean":+ return <BooleanControl {...props} />;+ case "array":+ return <TextControl {...props} />;+ case "color":+ return <TextControl {...props} type="color" />;+ case "date":+ return <TextControl {...props} type="date" />;+ case "number":+ return <TextControl {...props} type="number" />;+ case "range":+ return <NotImplemented />;+ case "object":+ return <TextControl {...props} />;+ case "radio":+ return <RadioControl {...props} />;+ case "inline-radio":+ return <RadioControl {...props} />;+ case "check":+ return <CheckboxControl {...props} />;+ case "inline-check":+ return <CheckboxControl {...props} />;+ case "select":+ return <SelectControl {...props} />;+ case "multi-select":+ return <CheckboxControl {...props} />;+ case "text":+ return <TextControl {...props} />;+ default: {+ const _exhaustiveCheck: never = props;+ return <NotImplemented />;+ }+ }+}diff --git a/app/designer/features/props-panel/props-panel.stories.tsx b/app/designer/features/props-panel/props-panel.stories.tsxnew file mode 100644index 0000000000..a9ba1a6863--- /dev/null+++ b/app/designer/features/props-panel/props-panel.stories.tsx@@ -0,0 +1,67 @@+import { ComponentStoryObj } from "@storybook/react";+import { componentsMeta, type UserProp } from "@webstudio-is/sdk";+import { PropsPanel } from "./props-panel";++export default {+ title: "Props Panel",+ component: PropsPanel,+};++export const NoProps: ComponentStoryObj<typeof PropsPanel> = {+ args: {+ selectedInstanceData: {+ id: "1",+ component: "Heading",+ cssRules: [],+ browserStyle: {},+ props: {+ id: "1",+ props: [],+ },+ },+ },+};++export const DefaultProps: ComponentStoryObj<typeof PropsPanel> = {+ args: {+ selectedInstanceData: {+ id: "2",+ component: "Button",+ cssRules: [],+ browserStyle: {},+ props: {+ id: "1",+ props: [+ {+ id: "disabled",+ prop: "disabled",+ value: true,+ },+ ],+ },+ },+ },+};++export const AllProps: ComponentStoryObj<typeof PropsPanel> = {+ args: {+ selectedInstanceData: {+ id: "3",+ component: "Heading",+ cssRules: [],+ browserStyle: {},+ props: {+ id: "2",+ props: Object.entries(componentsMeta["Heading"].argTypes).map(+ ([prop, value]) => {+ return {+ id: `${prop}`,+ prop,+ value: value.control.defaultValue,+ } as UserProp;+ }+ ),+ },+ },+ },+};diff --git a/app/designer/features/props-panel/props-panel.tsx b/app/designer/features/props-panel/props-panel.tsxindex 15134af16f..3d5a39ae57 100644--- a/app/designer/features/props-panel/props-panel.tsx+++ b/app/designer/features/props-panel/props-panel.tsx@@ -1,174 +1,16 @@-import { type ComponentProps } from "react";import {- type UserProp,- type Publish,componentsMeta,type Instance,+ type Publish,+ type UserProp,} from "@webstudio-is/sdk";+import { Control } from "~/designer/features/props-panel/control";import { CollapsibleSection, ComponentInfo } from "~/designer/shared/inspector";-import {- TextField,- Flex,- Label,- Button,- Box,- Radio,- RadioGroup,- Select,- Switch,-} from "~/shared/design-system";-import { PlusIcon, TrashIcon } from "~/shared/icons";import type { SelectedInstanceData } from "~/shared/canvas-components";+import { Box, Button, Grid, TextField } from "~/shared/design-system";+import { PlusIcon, TrashIcon } from "~/shared/icons";import { usePropsLogic } from "./use-props-logic";-type BaseControlProps = {- value?: UserProp["value"];- onValueChange: (value: UserProp["value"]) => void;-};--type TextControlProps = BaseControlProps & {- type?: ComponentProps<typeof TextField>["type"];- defaultValue?: UserProp["value"];-};--const TextControl = ({- value,- defaultValue,- type,- onValueChange,-}: TextControlProps) => (- <TextField- type={type}- variant="ghost"- placeholder="Value"- name="value"- value={String(value || defaultValue || "")}- onChange={(event) => {- onValueChange(event.target.value);- }}- />-);--type RadioControlProps = BaseControlProps & {- options: Array<string>;- defaultValue: UserProp["value"];-};--const RadioControl = ({- value,- options,- defaultValue,- onValueChange,-}: RadioControlProps) => (- <RadioGroup- css={{ flexDirection: "column" }}- name="value"- value={String(value || defaultValue || "")}- onValueChange={onValueChange}- >- {options.map((value) => (- <Flex align="center" gap="1" key={value}>- <Radio value={value} />- <Label>{value}</Label>- </Flex>- ))}- </RadioGroup>-);--type SelectControlProps = BaseControlProps & {- options: Array<string>;- defaultValue: UserProp["value"];-};--const SelectControl = ({- value,- options,- defaultValue,- onValueChange,-}: SelectControlProps) => (- <Select- name="value"- value={String(value || defaultValue || "")}- onChange={(event) => {- onValueChange(event.target.value);- }}- >- {options.map((optionValue) => (- <option value={optionValue} key={optionValue}>- {optionValue}- </option>- ))}- </Select>-);--type BooleanControlProps = BaseControlProps & {- defaultValue: boolean;-};--const BooleanControl = ({- value,- defaultValue,- onValueChange,-}: BooleanControlProps) => (- <Switch- name="value"- defaultChecked={defaultValue}- checked={value === true}- onCheckedChange={onValueChange}- />-);--const renderControl = ({- component,- prop,- ...props-}: BaseControlProps & {- component: Instance["component"];- prop: UserProp["prop"];-}) => {- const meta = componentsMeta[component];-- const argType = meta?.argTypes?.[prop as keyof typeof meta.argTypes];-- switch (argType?.control.type) {- case "text": {- return <TextControl {...props} defaultValue={argType.defaultValue} />;- }- case "number": {- return (- <TextControl- {...props}- defaultValue={argType.defaultValue}- type="number"- />- );- }- case "radio": {- return (- <RadioControl- {...props}- defaultValue={argType.defaultValue}- options={argType.options}- />- );- }- case "select": {- return (- <SelectControl- {...props}- defaultValue={argType.defaultValue}- options={argType.options}- />- );- }- case "boolean": {- return <BooleanControl {...props} defaultValue={argType.defaultValue} />;- }- }-- return <TextControl {...props} />;-};-type PropertyProps = {id: UserProp["id"];prop: UserProp["prop"];@@ -190,8 +32,15 @@ const Property = ({onChange,onDelete,}: PropertyProps) => {+ const meta = componentsMeta[component];+ const argType = meta?.argTypes?.[prop as keyof typeof meta.argTypes];+ const type = argType?.control.type;+ const defaultValue = argType?.control.defaultValue;+ const options = argType?.options;+ // TODO: We need to sync defaultValue with value if value is not set and publish this change+ // const realValue = value || defaultValue;return (- <Flex gap="1">+ <Grid gap="1" css={{ gridTemplateColumns: "auto auto 1fr auto" }}><TextFieldvariant="ghost"placeholder="Property"@@ -201,14 +50,14 @@ const Property = ({onChange(id, "prop", event.target.value);}}/>- {renderControl({- prop,- component,- value,- onValueChange(value: UserProp["prop"] | UserProp["value"]) {- onChange(id, "value", value);- },- })}+ :+ <Control+ type={type}+ defaultValue={defaultValue}+ options={options}+ value={value}+ onChange={(value) => onChange(id, "value", value)}+ /><ButtonghostonClick={() => {@@ -217,7 +66,7 @@ const Property = ({><TrashIcon /></Button>- </Flex>+ </Grid>);};