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.

Background title section (#1279)

Ivan Starkove5e22e6f6d03
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
index 53a6179511..2bf047c51a 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
@@ -146,7 +146,11 @@ export const BackgroundContent = (props: BackgroundContentProps) => {
<Separator css={{ gridColumn: "span 2" }} />
<BackgroundSection>
- <Grid css={{ gridTemplateColumns: "1fr 128px" }} align="center" gap={2}>
+ <Grid
+ css={{ gridTemplateColumns: `1fr ${theme.spacing[23]}` }}
+ align="center"
+ gap={2}
+ >
{imageGradientToggle === "image" && (
<>
<Label color="default" truncate>
@@ -239,7 +243,10 @@ export const BackgroundContent = (props: BackgroundContentProps) => {
/>
<Grid
- css={{ gridTemplateColumns: "1fr 128px", mt: theme.spacing[5] }}
+ css={{
+ gridTemplateColumns: `1fr ${theme.spacing[23]}`,
+ mt: theme.spacing[5],
+ }}
align="center"
gap={2}
>
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.ts b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.ts
index 6a97302000..db684ce6f1 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.ts
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-layers.ts
@@ -1,5 +1,9 @@
import type { LayersValue, StyleValue } from "@webstudio-is/css-data";
-import type { StyleInfo, StyleValueInfo } from "../../shared/style-info";
+import {
+ getStyleSource,
+ StyleInfo,
+ StyleValueInfo,
+} from "../../shared/style-info";
import type {
CreateBatchUpdate,
DeleteProperty,
@@ -152,6 +156,18 @@ const getLayersValue = (styleValue?: StyleValueInfo) => {
return { type: "layers" as const, value: [] };
};
+export const getLayersStyleSource = (style: StyleInfo) => {
+ return getStyleSource(...layeredBackgroundProps.map((prop) => style[prop]));
+};
+
+export const deleteLayers = (createBatchUpdate: CreateBatchUpdate) => {
+ const batch = createBatchUpdate();
+ for (const property of layeredBackgroundProps) {
+ batch.deleteProperty(property);
+ }
+ batch.publish();
+};
+
const isLayerStylesRecord = (value: {
[property in LayeredBackgroundProperty]?: LayersValue;
}): value is Record<LayeredBackgroundProperty, LayersValue> => {
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
index 34b7ca05f3..f0474f52b4 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
@@ -63,7 +63,11 @@ export const BackgroundSize = (
return (
<>
- <Grid css={{ gridTemplateColumns: "1fr 128px" }} align="center" gap={2}>
+ <Grid
+ css={{ gridTemplateColumns: `1fr ${theme.spacing[23]}` }}
+ align="center"
+ gap={2}
+ >
<Label color="default" truncate>
Size
</Label>
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.stories.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.stories.tsx
index ddce942452..0df7cbe7ae 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.stories.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.stories.tsx
@@ -1,5 +1,6 @@
import type { LayersValue } from "@webstudio-is/css-data";
import { styled, theme } from "@webstudio-is/design-system";
+import { setEnv } from "@webstudio-is/feature-flags";
import { useRef, useState } from "react";
import type { StyleInfo } from "../../shared/style-info";
import type {
@@ -9,6 +10,8 @@ import type {
} from "../../shared/use-style-data";
import { BackgroundsSection } from "./backgrounds";
+setEnv("*");
+
const backgroundImageStyle: LayersValue = {
type: "layers",
value: [
@@ -27,18 +30,11 @@ const backgroundImageStyle: LayersValue = {
],
};
-const styleInfoInitial: StyleInfo = {
- backgroundImage: {
- value: backgroundImageStyle,
- local: backgroundImageStyle,
- },
-};
-
const Panel = styled("div", {
width: theme.spacing[30],
});
-export const Backgrounds = () => {
+const useStyleInfo = (styleInfoInitial: StyleInfo) => {
const [styleInfo, setStyleInfo] = useState(() => styleInfoInitial);
const setProperty: SetProperty = (name) => (value, options) => {
@@ -94,6 +90,46 @@ export const Backgrounds = () => {
},
});
+ return { styleInfo, setProperty, deleteProperty, createBatchUpdate };
+};
+
+export const BackgroundsCollapsible = () => {
+ const { styleInfo, setProperty, deleteProperty, createBatchUpdate } =
+ useStyleInfo({
+ backgroundImage: {
+ cascaded: {
+ value: backgroundImageStyle,
+ breakpointId: "mobile",
+ },
+ value: backgroundImageStyle,
+ },
+ });
+
+ return (
+ <Panel>
+ <BackgroundsSection
+ currentStyle={styleInfo}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ createBatchUpdate={createBatchUpdate}
+ category={"backgrounds"}
+ styleConfigsByCategory={[]}
+ moreStyleConfigsByCategory={[]}
+ label="Backgrounds"
+ />
+ </Panel>
+ );
+};
+
+export const Backgrounds = () => {
+ const { styleInfo, setProperty, deleteProperty, createBatchUpdate } =
+ useStyleInfo({
+ backgroundImage: {
+ value: backgroundImageStyle,
+ local: backgroundImageStyle,
+ },
+ });
+
return (
<Panel>
<BackgroundsSection
@@ -104,6 +140,7 @@ export const Backgrounds = () => {
category={"backgrounds"}
styleConfigsByCategory={[]}
moreStyleConfigsByCategory={[]}
+ label="Backgrounds"
/>
</Panel>
);
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
index 5bea94654d..f0b1e0d016 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
@@ -2,10 +2,13 @@ import type { RenderCategoryProps } from "../../style-sections";
import { styleConfigByName } from "../../shared/configs";
import { FloatingPanel } from "~/builder/shared/floating-panel";
import {
- Button,
CssValueListItem,
Flex,
Grid,
+ Label,
+ SectionTitle,
+ SectionTitleButton,
+ SectionTitleLabel,
SmallIconButton,
theme,
} from "@webstudio-is/design-system";
@@ -14,6 +17,7 @@ import {
EyeconOpenIcon,
EyeconClosedIcon,
SubtractIcon,
+ PlusIcon,
} from "@webstudio-is/icons";
import { PropertyName } from "../../shared/property-name";
import type { StyleInfo } from "../../shared/style-info";
@@ -29,12 +33,18 @@ import {
getLayerBackgroundStyleInfo,
deleteLayerProperty,
swapLayers,
+ getLayersStyleSource,
+ deleteLayers,
} from "./background-layers";
import { BackgroundContent } from "./background-content";
import { getLayerName, LayerThumbnail } from "./background-thumbnail";
import { useSortable } from "./use-sortable";
import { useMemo } from "react";
import type { RgbValue } from "@webstudio-is/css-data";
+import {
+ CollapsibleSectionBase,
+ useOpenState,
+} from "~/builder/shared/collapsible-section";
const Layer = (props: {
id: string;
@@ -84,12 +94,9 @@ const Layer = (props: {
active={props.isHighlighted}
data-id={props.id}
label={
- <PropertyName
- style={props.layerStyle}
- property={layeredBackgroundProps}
- label={getLayerName(props.layerStyle)}
- onReset={props.deleteLayer}
- />
+ <Label truncate onReset={props.deleteLayer}>
+ {getLayerName(props.layerStyle)}
+ </Label>
}
thumbnail={<LayerThumbnail layerStyle={props.layerStyle} />}
hidden={isHidden}
@@ -117,12 +124,69 @@ const Layer = (props: {
);
};
-export const BackgroundsSection = ({
- setProperty,
- deleteProperty,
- currentStyle,
- createBatchUpdate,
-}: RenderCategoryProps) => {
+const BackgroundsCollapsibleSection = (
+ props: RenderCategoryProps & { children: React.ReactNode }
+) => {
+ const { label, children } = props;
+ const [isOpen, setIsOpen] = useOpenState(props);
+
+ if (props === undefined) {
+ // @todo will gone after refactor
+ throw new Error("Error");
+ }
+ const layersStyleSource = getLayersStyleSource(props.currentStyle);
+ const dots: ("local" | "remote")[] = [];
+
+ if (layersStyleSource === "local" || layersStyleSource === "remote") {
+ dots.push(layersStyleSource);
+ }
+
+ return (
+ <CollapsibleSectionBase
+ label={label}
+ fullWidth
+ isOpen={isOpen}
+ onOpenChange={(nextIsOpen) => {
+ setIsOpen(nextIsOpen);
+ }}
+ trigger={
+ <SectionTitle
+ dots={dots}
+ suffix={
+ <SectionTitleButton
+ prefix={<PlusIcon />}
+ onClick={() => {
+ const { currentStyle, createBatchUpdate } = props;
+ addLayer(currentStyle, createBatchUpdate);
+ setIsOpen(true);
+ }}
+ />
+ }
+ >
+ <PropertyName
+ style={props.currentStyle}
+ property={layeredBackgroundProps}
+ label={
+ <SectionTitleLabel color={layersStyleSource}>
+ {props.label}
+ </SectionTitleLabel>
+ }
+ onReset={() => {
+ const { createBatchUpdate } = props;
+ deleteLayers(createBatchUpdate);
+ }}
+ />
+ </SectionTitle>
+ }
+ >
+ {children}
+ </CollapsibleSectionBase>
+ );
+};
+
+export const BackgroundsSection = (props: RenderCategoryProps) => {
+ const { setProperty, deleteProperty, currentStyle, createBatchUpdate } =
+ props;
const layersCount = getLayerCount(currentStyle);
const { items } = styleConfigByName["backgroundColor"];
@@ -144,47 +208,66 @@ export const BackgroundsSection = ({
});
return (
- <Flex gap={1} direction="column">
- <Flex
- gap={1}
- direction="column"
- ref={sortableRefCallback}
- css={{
- pointerEvents: dragItemId ? "none" : "auto",
- // to make DnD work we have to disable scrolling using touch
- touchAction: "none",
- }}
- >
- {layers.map((layer) => (
- <Layer
- id={layer.id}
- key={layer.id}
- isHighlighted={dragItemId === layer.id}
- layerStyle={getLayerBackgroundStyleInfo(layer.index, currentStyle)}
- deleteLayer={deleteLayer(
- layer.index,
- currentStyle,
- createBatchUpdate
- )}
- setProperty={setLayerProperty(
- layer.index,
- currentStyle,
- createBatchUpdate
- )}
- deleteProperty={deleteLayerProperty(
- layer.index,
- currentStyle,
- deleteProperty,
- createBatchUpdate
- )}
- setBackgroundColor={setProperty("backgroundColor")}
- />
- ))}
+ <BackgroundsCollapsibleSection
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ createBatchUpdate={createBatchUpdate}
+ currentStyle={currentStyle}
+ category={props.category}
+ styleConfigsByCategory={props.styleConfigsByCategory}
+ moreStyleConfigsByCategory={props.moreStyleConfigsByCategory}
+ label={props.label}
+ isOpen={props.isOpen}
+ >
+ <Flex gap={1} direction="column">
+ <Flex
+ gap={1}
+ direction="column"
+ ref={sortableRefCallback}
+ css={{
+ pointerEvents: dragItemId ? "none" : "auto",
+ // to make DnD work we have to disable scrolling using touch
+ touchAction: "none",
+ }}
+ >
+ {layers.map((layer) => (
+ <Layer
+ id={layer.id}
+ key={layer.id}
+ isHighlighted={dragItemId === layer.id}
+ layerStyle={getLayerBackgroundStyleInfo(
+ layer.index,
+ currentStyle
+ )}
+ deleteLayer={deleteLayer(
+ layer.index,
+ currentStyle,
+ createBatchUpdate
+ )}
+ setProperty={setLayerProperty(
+ layer.index,
+ currentStyle,
+ createBatchUpdate
+ )}
+ deleteProperty={deleteLayerProperty(
+ layer.index,
+ currentStyle,
+ deleteProperty,
+ createBatchUpdate
+ )}
+ setBackgroundColor={setProperty("backgroundColor")}
+ />
+ ))}
- {placementIndicator}
- </Flex>
- <Flex css={{ px: theme.spacing[9] }} direction="column" gap={2}>
- <Grid css={{ gridTemplateColumns: "1fr 128px" }}>
+ {placementIndicator}
+ </Flex>
+
+ <Grid
+ css={{
+ px: theme.spacing[9],
+ gridTemplateColumns: `1fr ${theme.spacing[23]}`,
+ }}
+ >
<PropertyName
style={currentStyle}
property={"backgroundColor"}
@@ -200,15 +283,7 @@ export const BackgroundsSection = ({
deleteProperty={deleteProperty}
/>
</Grid>
- <Button
- color="neutral"
- onClick={() => {
- addLayer(currentStyle, createBatchUpdate);
- }}
- >
- Add layer
- </Button>
</Flex>
- </Flex>
+ </BackgroundsCollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/borders/borders.stories.tsx b/apps/builder/app/builder/features/style-panel/sections/borders/borders.stories.tsx
index 20c841a696..7bf9b10c33 100644
--- a/apps/builder/app/builder/features/style-panel/sections/borders/borders.stories.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/borders/borders.stories.tsx
@@ -87,6 +87,7 @@ export const Borders = () => {
deleteProperty={deleteProperty}
createBatchUpdate={createBatchUpdate}
category={"borders"}
+ label={"Borders"}
styleConfigsByCategory={[]}
moreStyleConfigsByCategory={[]}
/>
diff --git a/apps/builder/app/builder/features/style-panel/sections/borders/borders.tsx b/apps/builder/app/builder/features/style-panel/sections/borders/borders.tsx
index 2d527148dc..03b47f8750 100644
--- a/apps/builder/app/builder/features/style-panel/sections/borders/borders.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/borders/borders.tsx
@@ -1,4 +1,5 @@
import { Box, Flex, Grid, theme } from "@webstudio-is/design-system";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
import { ColorControl } from "../../controls";
import { styleConfigByName } from "../../shared/configs";
import { PropertyName } from "../../shared/property-name";
@@ -14,60 +15,64 @@ export const BordersSection = ({
setProperty,
deleteProperty,
createBatchUpdate,
+ label,
+ isOpen,
}: RenderCategoryProps) => {
return (
- <Flex direction="column" gap={2}>
- <Grid
- css={{
- // Our aim is to maintain consistent styling throughout the property and align
- // the input fields on the left-hand side
- // See ./border-property.tsx for more details
- gridTemplateColumns: `1fr ${theme.spacing[20]} ${theme.spacing[12]}`,
- }}
- gapX={2}
- >
- <PropertyName
- style={currentStyle}
- property={"borderTopColor"}
- label={"Color"}
- onReset={() => deleteProperty("borderTopColor")}
- />
-
- <Box
+ <CollapsibleSection label={label} isOpen={isOpen}>
+ <Flex direction="column" gap={2}>
+ <Grid
css={{
- gridColumn: `span 2`,
+ // Our aim is to maintain consistent styling throughout the property and align
+ // the input fields on the left-hand side
+ // See ./border-property.tsx for more details
+ gridTemplateColumns: `1fr ${theme.spacing[20]} ${theme.spacing[12]}`,
}}
+ gapX={2}
>
- <ColorControl
+ <PropertyName
+ style={currentStyle}
property={"borderTopColor"}
- items={borderColorItems}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
+ label={"Color"}
+ onReset={() => deleteProperty("borderTopColor")}
/>
- </Box>
- </Grid>
- <BorderStyle
- createBatchUpdate={createBatchUpdate}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <Box
+ css={{
+ gridColumn: `span 2`,
+ }}
+ >
+ <ColorControl
+ property={"borderTopColor"}
+ items={borderColorItems}
+ currentStyle={currentStyle}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ </Box>
+ </Grid>
+
+ <BorderStyle
+ createBatchUpdate={createBatchUpdate}
+ currentStyle={currentStyle}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
- <BorderWidth
- createBatchUpdate={createBatchUpdate}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
+ <BorderWidth
+ createBatchUpdate={createBatchUpdate}
+ currentStyle={currentStyle}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
- <BorderRadius
- createBatchUpdate={createBatchUpdate}
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- </Flex>
+ <BorderRadius
+ createBatchUpdate={createBatchUpdate}
+ currentStyle={currentStyle}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ </Flex>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/effects/effects.tsx b/apps/builder/app/builder/features/style-panel/sections/effects/effects.tsx
index 59cb2fc213..9c8fc97836 100644
--- a/apps/builder/app/builder/features/style-panel/sections/effects/effects.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/effects/effects.tsx
@@ -1,19 +1,22 @@
import type { RenderCategoryProps } from "../../style-sections";
import { ShowMore } from "../../shared/show-more";
import { renderProperty } from "../../style-sections";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
export const EffectsSection = ({
styleConfigsByCategory,
moreStyleConfigsByCategory,
+ label,
+ isOpen,
}: RenderCategoryProps) => {
return (
- <>
+ <CollapsibleSection label={label} isOpen={isOpen}>
{styleConfigsByCategory.map((entry) => renderProperty(entry))}
<ShowMore
styleConfigs={moreStyleConfigsByCategory.map((entry) =>
renderProperty(entry)
)}
/>
- </>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/flex-child/flex-child.tsx b/apps/builder/app/builder/features/style-panel/sections/flex-child/flex-child.tsx
index 92ca440c2c..39bdf4536f 100644
--- a/apps/builder/app/builder/features/style-panel/sections/flex-child/flex-child.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/flex-child/flex-child.tsx
@@ -20,14 +20,17 @@ import {
import { FloatingPanel } from "~/builder/shared/floating-panel";
import { getStyleSource } from "../../shared/style-info";
import { theme } from "@webstudio-is/design-system";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
export const FlexChildSection = (props: RenderCategoryProps) => {
return (
- <Flex css={{ flexDirection: "column", gap: theme.spacing[5] }}>
- <FlexChildSectionAlign {...props} />
- <FlexChildSectionSizing {...props} />
- <FlexChildSectionOrder {...props} />
- </Flex>
+ <CollapsibleSection label={props.label} isOpen={props.isOpen}>
+ <Flex css={{ flexDirection: "column", gap: theme.spacing[5] }}>
+ <FlexChildSectionAlign {...props} />
+ <FlexChildSectionSizing {...props} />
+ <FlexChildSectionOrder {...props} />
+ </Flex>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/grid-child/grid-child.tsx b/apps/builder/app/builder/features/style-panel/sections/grid-child/grid-child.tsx
index 543ed0e8e5..8585917311 100644
--- a/apps/builder/app/builder/features/style-panel/sections/grid-child/grid-child.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/grid-child/grid-child.tsx
@@ -1,19 +1,22 @@
import type { RenderCategoryProps } from "../../style-sections";
import { ShowMore } from "../../shared/show-more";
import { renderProperty } from "../../style-sections";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
export const GridChildSection = ({
styleConfigsByCategory,
moreStyleConfigsByCategory,
+ label,
+ isOpen,
}: RenderCategoryProps) => {
return (
- <>
+ <CollapsibleSection label={label} isOpen={isOpen}>
{styleConfigsByCategory.map((entry) => renderProperty(entry))}
<ShowMore
styleConfigs={moreStyleConfigsByCategory.map((entry) =>
renderProperty(entry)
)}
/>
- </>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/layout/layout.tsx b/apps/builder/app/builder/features/style-panel/sections/layout/layout.tsx
index baea960f6a..d947dc2cac 100644
--- a/apps/builder/app/builder/features/style-panel/sections/layout/layout.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/layout/layout.tsx
@@ -28,6 +28,7 @@ import {
CssValueInput,
} from "../../shared/css-value-input";
import { theme } from "@webstudio-is/design-system";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
const GapLinked = ({
@@ -348,45 +349,49 @@ export const LayoutSection = ({
deleteProperty,
createBatchUpdate,
styleConfigsByCategory,
+ label: sectionLabel,
+ isOpen,
}: RenderCategoryProps) => {
const displayValue = toValue(currentStyle.display?.value);
const { label, items } = styleConfigByName.display;
return (
- <>
- <Grid css={{ gridTemplateColumns: "4fr 6fr" }}>
- <PropertyName
- style={currentStyle}
- property="display"
- label={label}
- onReset={() => deleteProperty("display")}
- />
- <SelectControl
- property="display"
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- // show only important values first and hide others with scroll
- items={items
- .filter((item) => orderedDisplayValues.includes(item.name))
- .sort(compareDisplayValues)}
- />
- </Grid>
+ <CollapsibleSection label={sectionLabel} isOpen={isOpen}>
+ <>
+ <Grid css={{ gridTemplateColumns: "4fr 6fr" }}>
+ <PropertyName
+ style={currentStyle}
+ property="display"
+ label={label}
+ onReset={() => deleteProperty("display")}
+ />
+ <SelectControl
+ property="display"
+ currentStyle={currentStyle}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ // show only important values first and hide others with scroll
+ items={items
+ .filter((item) => orderedDisplayValues.includes(item.name))
+ .sort(compareDisplayValues)}
+ />
+ </Grid>
- {displayValue === "flex" || displayValue === "inline-flex" ? (
- <LayoutSectionFlex
- currentStyle={currentStyle}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- createBatchUpdate={createBatchUpdate}
- />
- ) : (
- styleConfigsByCategory.map((entry) =>
- // exclude display already rendered above
- entry.property === "display" ? null : renderProperty(entry)
- )
- )}
- </>
+ {displayValue === "flex" || displayValue === "inline-flex" ? (
+ <LayoutSectionFlex
+ currentStyle={currentStyle}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ createBatchUpdate={createBatchUpdate}
+ />
+ ) : (
+ styleConfigsByCategory.map((entry) =>
+ // exclude display already rendered above
+ entry.property === "display" ? null : renderProperty(entry)
+ )
+ )}
+ </>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/other/other.tsx b/apps/builder/app/builder/features/style-panel/sections/other/other.tsx
index ac4b9548b7..0f61e3bed7 100644
--- a/apps/builder/app/builder/features/style-panel/sections/other/other.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/other/other.tsx
@@ -1,19 +1,22 @@
import type { RenderCategoryProps } from "../../style-sections";
import { ShowMore } from "../../shared/show-more";
import { renderProperty } from "../../style-sections";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
export const OtherSection = ({
styleConfigsByCategory,
moreStyleConfigsByCategory,
+ label,
+ isOpen,
}: RenderCategoryProps) => {
return (
- <>
+ <CollapsibleSection label={label} isOpen={isOpen}>
{styleConfigsByCategory.map((entry) => renderProperty(entry))}
<ShowMore
styleConfigs={moreStyleConfigsByCategory.map((entry) =>
renderProperty(entry)
)}
/>
- </>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/position/position.tsx b/apps/builder/app/builder/features/style-panel/sections/position/position.tsx
index 41fc379430..2e01548d93 100644
--- a/apps/builder/app/builder/features/style-panel/sections/position/position.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/position/position.tsx
@@ -1,19 +1,22 @@
import type { RenderCategoryProps } from "../../style-sections";
import { ShowMore } from "../../shared/show-more";
import { renderProperty } from "../../style-sections";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
export const PositionSection = ({
styleConfigsByCategory,
moreStyleConfigsByCategory,
+ label,
+ isOpen,
}: RenderCategoryProps) => {
return (
- <>
+ <CollapsibleSection label={label} isOpen={isOpen}>
{styleConfigsByCategory.map((entry) => renderProperty(entry))}
<ShowMore
styleConfigs={moreStyleConfigsByCategory.map((entry) =>
renderProperty(entry)
)}
/>
- </>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/size/size.tsx b/apps/builder/app/builder/features/style-panel/sections/size/size.tsx
index 0208fbe95b..1e2732cef3 100644
--- a/apps/builder/app/builder/features/style-panel/sections/size/size.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/size/size.tsx
@@ -18,6 +18,7 @@ import {
import { getStyleSource } from "../../shared/style-info";
import { theme } from "@webstudio-is/design-system";
import { ToggleGroupControl } from "../../controls/toggle/toggle-control";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
const SizeField = ({
property,
@@ -53,119 +54,123 @@ export const SizeSection = ({
currentStyle: style,
setProperty,
deleteProperty,
+ label,
+ isOpen,
}: RenderCategoryProps) => {
return (
- <Grid
- columns={2}
- css={{ columnGap: theme.spacing[5], rowGap: theme.spacing[7] }}
- >
- <SizeField
- property="width"
- style={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeField
- property="height"
- style={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeField
- property="minWidth"
- style={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeField
- property="minHeight"
- style={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeField
- property="maxWidth"
- style={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <SizeField
- property="maxHeight"
- style={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <PropertyName
- label={styleConfigByName.aspectRatio.label}
- property="aspectRatio"
- style={style}
- onReset={() => deleteProperty("aspectRatio")}
- />
- <TextControl
- property={"aspectRatio"}
- currentStyle={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <PropertyName
- label={styleConfigByName.overflow.label}
- property="overflow"
- style={style}
- onReset={() => deleteProperty("overflow")}
- />
- <ToggleGroupControl
- styleSource={getStyleSource(style.overflow)}
- items={[
- {
- child: <EyeconOpenIcon />,
- label: "Visible",
- value: "visible",
- },
- {
- child: <EyeconClosedIcon />,
- label: "Hidden",
- value: "hidden",
- },
- {
- child: <ScrollIcon />,
- label: "Scroll",
- value: "scroll",
- },
- {
- child: <AutoScrollIcon />,
- label: "Auto",
- value: "auto",
- },
- ]}
- value={toValue(style.overflow?.value)}
- onValueChange={(value) =>
- setProperty("overflow")({ type: "keyword", value })
- }
- />
- <PropertyName
- label={styleConfigByName.objectFit.label}
- property="objectFit"
- style={style}
- onReset={() => deleteProperty("objectFit")}
- />
- <SelectControl
- property="objectFit"
- currentStyle={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- <PropertyName
- label={styleConfigByName.objectPosition.label}
- property="objectPosition"
- style={style}
- onReset={() => deleteProperty("objectPosition")}
- />
- <ObjectPositionControl
- property="objectPosition"
- currentStyle={style}
- setProperty={setProperty}
- deleteProperty={deleteProperty}
- />
- </Grid>
+ <CollapsibleSection label={label} isOpen={isOpen}>
+ <Grid
+ columns={2}
+ css={{ columnGap: theme.spacing[5], rowGap: theme.spacing[7] }}
+ >
+ <SizeField
+ property="width"
+ style={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ <SizeField
+ property="height"
+ style={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ <SizeField
+ property="minWidth"
+ style={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ <SizeField
+ property="minHeight"
+ style={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ <SizeField
+ property="maxWidth"
+ style={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ <SizeField
+ property="maxHeight"
+ style={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ <PropertyName
+ label={styleConfigByName.aspectRatio.label}
+ property="aspectRatio"
+ style={style}
+ onReset={() => deleteProperty("aspectRatio")}
+ />
+ <TextControl
+ property={"aspectRatio"}
+ currentStyle={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ <PropertyName
+ label={styleConfigByName.overflow.label}
+ property="overflow"
+ style={style}
+ onReset={() => deleteProperty("overflow")}
+ />
+ <ToggleGroupControl
+ styleSource={getStyleSource(style.overflow)}
+ items={[
+ {
+ child: <EyeconOpenIcon />,
+ label: "Visible",
+ value: "visible",
+ },
+ {
+ child: <EyeconClosedIcon />,
+ label: "Hidden",
+ value: "hidden",
+ },
+ {
+ child: <ScrollIcon />,
+ label: "Scroll",
+ value: "scroll",
+ },
+ {
+ child: <AutoScrollIcon />,
+ label: "Auto",
+ value: "auto",
+ },
+ ]}
+ value={toValue(style.overflow?.value)}
+ onValueChange={(value) =>
+ setProperty("overflow")({ type: "keyword", value })
+ }
+ />
+ <PropertyName
+ label={styleConfigByName.objectFit.label}
+ property="objectFit"
+ style={style}
+ onReset={() => deleteProperty("objectFit")}
+ />
+ <SelectControl
+ property="objectFit"
+ currentStyle={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ <PropertyName
+ label={styleConfigByName.objectPosition.label}
+ property="objectPosition"
+ style={style}
+ onReset={() => deleteProperty("objectPosition")}
+ />
+ <ObjectPositionControl
+ property="objectPosition"
+ currentStyle={style}
+ setProperty={setProperty}
+ deleteProperty={deleteProperty}
+ />
+ </Grid>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/space/space.tsx b/apps/builder/app/builder/features/style-panel/sections/space/space.tsx
index 14ccc80426..0e9507ff5b 100644
--- a/apps/builder/app/builder/features/style-panel/sections/space/space.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/space/space.tsx
@@ -9,6 +9,7 @@ import { InputPopover } from "./input-popover";
import { SpaceTooltip } from "./tooltip";
import { getStyleSource } from "../../shared/style-info";
import { useKeyboardNavigation } from "./keyboard";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
const Cell = ({
isPopoverOpen,
@@ -90,6 +91,8 @@ export const SpaceSection = ({
deleteProperty,
createBatchUpdate,
currentStyle,
+ label,
+ isOpen,
}: RenderCategoryProps) => {
const [hoverTarget, setHoverTarget] = useState<HoverTagret>();
@@ -138,38 +141,40 @@ export const SpaceSection = ({
};
return (
- <SpaceLayout
- ref={layoutRef}
- onClick={() => setOpenProperty(hoverTarget?.property)}
- onHover={handleHover}
- onFocus={keyboardNavigation.hadnleFocus}
- onBlur={keyboardNavigation.handleBlur}
- onKeyDown={keyboardNavigation.handleKeyDown}
- onMouseLeave={keyboardNavigation.handleMouseLeave}
- activeProperties={activeProperties}
- renderCell={({ property }) => (
- <Cell
- isPopoverOpen={openProperty === property}
- onPopoverClose={() => {
- if (openProperty === property) {
- setOpenProperty(undefined);
- layoutRef.current?.focus();
- }
- }}
- onChange={(update, options) => {
- if (update.operation === "set") {
- setProperty(update.property)(update.value, options);
- } else {
- deleteProperty(update.property, options);
- }
- }}
- onHover={handleHover}
- property={property}
- scrubStatus={scrubStatus}
- isActive={activeProperties.includes(property)}
- currentStyle={currentStyle}
- />
- )}
- />
+ <CollapsibleSection label={label} isOpen={isOpen}>
+ <SpaceLayout
+ ref={layoutRef}
+ onClick={() => setOpenProperty(hoverTarget?.property)}
+ onHover={handleHover}
+ onFocus={keyboardNavigation.hadnleFocus}
+ onBlur={keyboardNavigation.handleBlur}
+ onKeyDown={keyboardNavigation.handleKeyDown}
+ onMouseLeave={keyboardNavigation.handleMouseLeave}
+ activeProperties={activeProperties}
+ renderCell={({ property }) => (
+ <Cell
+ isPopoverOpen={openProperty === property}
+ onPopoverClose={() => {
+ if (openProperty === property) {
+ setOpenProperty(undefined);
+ layoutRef.current?.focus();
+ }
+ }}
+ onChange={(update, options) => {
+ if (update.operation === "set") {
+ setProperty(update.property)(update.value, options);
+ } else {
+ deleteProperty(update.property, options);
+ }
+ }}
+ onHover={handleHover}
+ property={property}
+ scrubStatus={scrubStatus}
+ isActive={activeProperties.includes(property)}
+ currentStyle={currentStyle}
+ />
+ )}
+ />
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/typography/typography.tsx b/apps/builder/app/builder/features/style-panel/sections/typography/typography.tsx
index 3c1943b5e3..a71f1ff9c2 100644
--- a/apps/builder/app/builder/features/style-panel/sections/typography/typography.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/typography/typography.tsx
@@ -36,14 +36,17 @@ import { ToggleGroupControl } from "../../controls/toggle/toggle-control";
import { FloatingPanel } from "~/builder/shared/floating-panel";
import { getStyleSource } from "../../shared/style-info";
import { theme } from "@webstudio-is/design-system";
+import { CollapsibleSection } from "~/builder/shared/collapsible-section";
export const TypographySection = (props: RenderCategoryProps) => {
return (
- <Flex css={{ gap: theme.spacing[7] }} direction="column">
- <TypographySectionFont {...props} />
- <TypographySectionSizing {...props} />
- <TypographySectionAdvanced {...props} />
- </Flex>
+ <CollapsibleSection label={props.label} isOpen={props.isOpen}>
+ <Flex css={{ gap: theme.spacing[7] }} direction="column">
+ <TypographySectionFont {...props} />
+ <TypographySectionSizing {...props} />
+ <TypographySectionAdvanced {...props} />
+ </Flex>
+ </CollapsibleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/shared/property-name.tsx b/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
index cac541e18e..6ec872f4e9 100644
--- a/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
@@ -1,4 +1,4 @@
-import { useState } from "react";
+import { useState, type ReactElement, type MouseEventHandler } from "react";
import { useStore } from "@nanostores/react";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import type { StyleProperty } from "@webstudio-is/css-data";
@@ -30,7 +30,7 @@ const PropertyPopoverContent = ({
properties: StyleProperty[];
style: StyleInfo;
styleSource: StyleSource;
- onReset: () => void;
+ onReset: MouseEventHandler<HTMLButtonElement>;
}) => {
const [breakpoints] = useBreakpoints();
const instances = useStore(instancesStore);
@@ -123,8 +123,8 @@ const PropertyPopoverContent = ({
type PropertyNameProps = {
style: StyleInfo;
property: StyleProperty | StyleProperty[];
- label: string;
- onReset: () => void;
+ label: string | ReactElement;
+ onReset: React.MouseEventHandler<HTMLButtonElement>;
};
export const PropertyName = ({
@@ -142,13 +142,16 @@ export const PropertyName = ({
isFeatureEnabled("propertyReset") &&
(styleSource === "local" || styleSource === "remote");
- const labelElement = (
- <Flex shrink>
- <Label color={styleSource} truncate>
- {label}
- </Label>
- </Flex>
- );
+ const labelElement =
+ typeof label === "string" ? (
+ <Flex shrink>
+ <Label color={styleSource} truncate>
+ {label}
+ </Label>
+ </Flex>
+ ) : (
+ label
+ );
if (isPopoverEnabled) {
return (
@@ -180,13 +183,19 @@ export const PropertyName = ({
return (
<Flex align="center">
- <Tooltip
- content={label}
- delayDuration={600}
- disableHoverableContent={true}
- >
- {labelElement}
- </Tooltip>
+ {typeof label === "string" ? (
+ <Tooltip
+ content={label}
+ delayDuration={600}
+ disableHoverableContent={true}
+ >
+ {labelElement}
+ </Tooltip>
+ ) : (
+ // It's on purpose to not wrap labelElement in Tooltip,
+ // it can be a complex element with its own tooltip or no tooltip at all like SectionTitle
+ labelElement
+ )}
</Flex>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/style-sections.tsx b/apps/builder/app/builder/features/style-panel/style-sections.tsx
index f9c25961aa..a6f4e80be8 100644
--- a/apps/builder/app/builder/features/style-panel/style-sections.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-sections.tsx
@@ -42,6 +42,8 @@ export type RenderCategoryProps = {
category: Category;
styleConfigsByCategory: Array<RenderPropertyProps>;
moreStyleConfigsByCategory: Array<RenderPropertyProps>;
+ label: string;
+ isOpen?: boolean | undefined;
};
export type RenderPropertyProps = {
@@ -92,8 +94,11 @@ export const renderCategory = ({
category,
styleConfigsByCategory,
moreStyleConfigsByCategory,
+ label,
+ isOpen,
}: RenderCategoryProps) => {
const Section = sections[category];
+
return (
<Section
setProperty={setProperty}
@@ -103,6 +108,8 @@ export const renderCategory = ({
category={category}
styleConfigsByCategory={styleConfigsByCategory}
moreStyleConfigsByCategory={moreStyleConfigsByCategory}
+ label={label}
+ isOpen={isOpen}
/>
);
};
@@ -121,7 +128,7 @@ export const shouldRenderCategory = (
return true;
};
-const sections: {
+export const sections: {
[Property in Category]: (props: RenderCategoryProps) => JSX.Element | null;
} = {
layout: LayoutSection,
diff --git a/apps/builder/app/builder/features/style-panel/style-settings.tsx b/apps/builder/app/builder/features/style-panel/style-settings.tsx
index 19ebccede7..eb76fe3a25 100644
--- a/apps/builder/app/builder/features/style-panel/style-settings.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-settings.tsx
@@ -2,14 +2,16 @@ import hyphenate from "hyphenate-style-name";
import { categories, type Category } from "@webstudio-is/react-sdk";
import type { StyleProperty } from "@webstudio-is/css-data";
import { toValue } from "@webstudio-is/css-engine";
-
import {
type StyleConfig,
styleConfigs,
styleConfigByName,
} from "./shared/configs";
-import { CollapsibleSection } from "~/builder/shared/collapsible-section";
-import { renderCategory, shouldRenderCategory } from "./style-sections";
+import {
+ renderCategory,
+ shouldRenderCategory,
+ type RenderCategoryProps,
+} from "./style-sections";
import { dependencies } from "./shared/dependencies";
import type { SetProperty, CreateBatchUpdate } from "./shared/use-style-data";
import type { StyleInfo } from "./shared/style-info";
@@ -17,6 +19,7 @@ import type { RenderPropertyProps } from "./style-sections";
import { useStore } from "@nanostores/react";
import { selectedInstanceSelectorStore } from "~/shared/nano-states";
import { useInstanceStyleData } from "./shared/style-info";
+import React from "react";
// Finds a property/value by using any available form: property, label, value
const filterProperties = (
@@ -164,7 +167,7 @@ export const StyleSettings = ({
if (styleConfigsByCategory.length === 0) {
continue;
}
- const categoryProps = {
+ const categoryProps: RenderCategoryProps = {
setProperty,
deleteProperty,
createBatchUpdate,
@@ -172,18 +175,15 @@ export const StyleSettings = ({
category,
styleConfigsByCategory,
moreStyleConfigsByCategory,
+ label: categories[category].label,
+ isOpen: isSearchMode ? true : undefined,
};
if (shouldRenderCategory(categoryProps, parentStyle)) {
all.push(
- <CollapsibleSection
- isOpen={isSearchMode ? true : undefined}
- label={categories[category].label}
- key={category}
- fullWidth={category === "backgrounds"}
- >
- <>{renderCategory(categoryProps)}</>
- </CollapsibleSection>
+ <React.Fragment key={category}>
+ {renderCategory(categoryProps)}
+ </React.Fragment>
);
}
}
diff --git a/apps/builder/app/builder/shared/collapsible-section.tsx b/apps/builder/app/builder/shared/collapsible-section.tsx
index c042618692..7d56b41273 100644
--- a/apps/builder/app/builder/shared/collapsible-section.tsx
+++ b/apps/builder/app/builder/shared/collapsible-section.tsx
@@ -11,6 +11,7 @@ import {
import { theme } from "@webstudio-is/design-system";
import type { ComponentProps, ReactNode } from "react";
import { PlusIcon } from "@webstudio-is/icons";
+import type { Simplify } from "type-fest";
type UseOpenStateProps = {
label: string;
@@ -21,7 +22,7 @@ type UseOpenStateProps = {
const stateContainer = atom<{ [label: string]: boolean }>({});
// Preserves the open/close state even when component gets unmounted
-const useOpenState = ({
+export const useOpenState = ({
label,
isOpenDefault = true,
isOpen: isOpenForced,
@@ -34,7 +35,7 @@ const useOpenState = ({
return [isOpenForced === undefined ? isOpen : isOpenForced, setIsOpen];
};
-type BaseProps = {
+type CollapsibleSectionBaseProps = {
trigger?: ReactNode;
children: ReactNode;
fullWidth?: boolean;
@@ -43,14 +44,14 @@ type BaseProps = {
onOpenChange: (value: boolean) => void;
};
-const CollapsibleSectionBase = ({
+export const CollapsibleSectionBase = ({
label,
trigger,
children,
fullWidth = false,
isOpen,
onOpenChange,
-}: BaseProps) => (
+}: CollapsibleSectionBaseProps) => (
<Collapsible.Root open={isOpen} onOpenChange={onOpenChange}>
<Box css={{ boxShadow: `0px 1px 0 ${theme.colors.panelOutline}` }}>
<Collapsible.Trigger asChild>
@@ -79,8 +80,10 @@ const CollapsibleSectionBase = ({
</Collapsible.Root>
);
-type CollapsibleSectionProps = Omit<BaseProps, "isOpen" | "onOpenChange"> &
- UseOpenStateProps;
+type CollapsibleSectionProps = Simplify<
+ Omit<CollapsibleSectionBaseProps, "isOpen" | "onOpenChange"> &
+ UseOpenStateProps
+>;
export const CollapsibleSection = (props: CollapsibleSectionProps) => {
const { label, trigger, children, fullWidth } = props;
@@ -102,7 +105,7 @@ export const CollapsibleSectionWithAddButton = ({
onAdd,
hasItems = true,
...props
-}: Omit<CollapsibleSectionProps, "trigger"> & {
+}: Omit<CollapsibleSectionProps, "trigger" | "categoryProps"> & {
onAdd: () => void;
/**
@@ -111,7 +114,7 @@ export const CollapsibleSectionWithAddButton = ({
*/
hasItems?: boolean | ComponentProps<typeof SectionTitle>["dots"];
}) => {
- const { label, children, fullWidth } = props;
+ const { label, children } = props;
const [isOpen, setIsOpen] = useOpenState(props);
const isEmpty =
@@ -123,7 +126,7 @@ export const CollapsibleSectionWithAddButton = ({
return (
<CollapsibleSectionBase
label={label}
- fullWidth={fullWidth}
+ fullWidth={false}
isOpen={isOpenFinal}
onOpenChange={(nextIsOpen) => {
setIsOpen(nextIsOpen);
diff --git a/apps/builder/package.json b/apps/builder/package.json
index e86a38ab96..05e6eae9e4 100644
--- a/apps/builder/package.json
+++ b/apps/builder/package.json
@@ -134,6 +134,7 @@
"prettier": "^2.8.4",
"react-test-renderer": "^17.0.2",
"relative-deps": "^1.0.7",
+ "type-fest": "^3.7.1",
"typescript": "4.9.5"
},
"sideEffects": false,
diff --git a/packages/design-system/src/components/label.stories.tsx b/packages/design-system/src/components/label.stories.tsx
index 9a25b439f2..b447e1626b 100644
--- a/packages/design-system/src/components/label.stories.tsx
+++ b/packages/design-system/src/components/label.stories.tsx
@@ -1,6 +1,9 @@
import type { ComponentStory } from "@storybook/react";
import { StorySection, StoryGrid } from "./storybook";
import { Label } from "./label";
+import { Box } from "./box";
+import * as Popover from "@radix-ui/react-popover";
+import { theme } from "../stitches.config";
export default {
title: "Library/Label",
@@ -16,9 +19,35 @@ const LabelStory: ComponentStory<typeof Label> = ({
return (
<>
<StorySection title="Configurable">
- <Label color={color} disabled={disabled}>
- {children}
- </Label>
+ <Box>
+ <Label color={color} disabled={disabled}>
+ {children}
+ </Label>
+ </Box>
+ </StorySection>
+
+ {/* Check that spacebar is working if color property is not default */}
+ <StorySection title="Configurable with Popover if color is not default">
+ <Box>
+ <Popover.Root>
+ <Popover.Trigger asChild>
+ <Label color={color} disabled={disabled}>
+ {children}
+ </Label>
+ </Popover.Trigger>
+ <Popover.Content side="bottom" align="center">
+ <Box
+ css={{
+ backgroundColor: theme.colors.backgroundPanel,
+ p: theme.spacing[10],
+ my: theme.spacing[2],
+ }}
+ >
+ <input type="text" />
+ </Box>
+ </Popover.Content>
+ </Popover.Root>
+ </Box>
</StorySection>
<StorySection title="Colors">
@@ -43,11 +72,7 @@ const LabelStory: ComponentStory<typeof Label> = ({
<StorySection title="Focused (initially)">
<StoryGrid horizontal>
- <Label
- color="local"
- tabIndex={-1}
- ref={(element) => element?.focus()}
- >
+ <Label color="local" ref={(element) => element?.focus()}>
Local
</Label>
</StoryGrid>
diff --git a/packages/design-system/src/components/label.tsx b/packages/design-system/src/components/label.tsx
index d7179cb603..3682dc40e3 100644
--- a/packages/design-system/src/components/label.tsx
+++ b/packages/design-system/src/components/label.tsx
@@ -7,10 +7,16 @@ import type { ComponentProps, ReactNode, Ref } from "react";
import { forwardRef } from "react";
import { textVariants } from "./text";
import { styled, theme } from "../stitches.config";
+import { Label as RadixLabel } from "@radix-ui/react-label";
export const labelColors = ["default", "preset", "local", "remote"] as const;
-const StyledLabel = styled("label", {
+const StyledLabel = styled(RadixLabel, {
+ all: "unset", // reset <button>
+ margin: 0,
+ WebkitAppearance: "none",
+ display: "block",
+
boxSizing: "border-box",
flexShrink: 0,
py: theme.spacing[1],
@@ -28,6 +34,14 @@ const StyledLabel = styled("label", {
},
variants: {
+ // The "display: inline" property can cause sizing issues with the label in certain scenarios.
+ // However, in our case, the label is being used as a button.
+ // To ensure compatibility with form labels, we only set the "inline" property if the "htmlFor" attribute is present.
+ hasHtmlFor: {
+ true: {
+ display: "inline",
+ },
+ },
color: {
default: {
color: theme.colors.foregroundMain,
@@ -81,19 +95,36 @@ const StyledLabel = styled("label", {
},
});
-type Props = ComponentProps<typeof StyledLabel> & {
+type Props = {
color?: (typeof labelColors)[number];
sectionTitle?: boolean;
disabled?: boolean;
truncate?: boolean;
children: ReactNode;
-};
+} & ComponentProps<typeof StyledLabel>;
+
+export const isLabelButton = (color: Props["color"]) =>
+ color === "preset" || color === "local" || color === "remote";
export const Label = forwardRef((props: Props, ref: Ref<HTMLLabelElement>) => {
const { disabled, children, ...rest } = props;
+
+ // To enable keyboard accessibility for users who rely on the spacebar to activate the radix
+ // when using a preset, local, or remote color, we need to wrap the label with a button that has a "label" role.
+ // (Radix adds role="button" to the label)
+ const isButton = isLabelButton(props.color);
+
return (
- <StyledLabel ref={ref} aria-disabled={disabled} {...rest}>
- {children}
+ <StyledLabel
+ ref={ref}
+ asChild={isButton}
+ // Label is exluded from tab order by default
+ tabIndex={props.tabIndex ?? (isButton ? -1 : undefined)}
+ hasHtmlFor={props.htmlFor !== undefined}
+ aria-disabled={disabled}
+ {...rest}
+ >
+ {isButton ? <button>{children}</button> : children}
</StyledLabel>
);
});
diff --git a/packages/design-system/src/components/section-title.stories.tsx b/packages/design-system/src/components/section-title.stories.tsx
index 277dff802f..844751dabd 100644
--- a/packages/design-system/src/components/section-title.stories.tsx
+++ b/packages/design-system/src/components/section-title.stories.tsx
@@ -49,12 +49,7 @@ const Variants = ({
suffix={<SectionTitleButton prefix={<PlusIcon />} />}
data-state={state}
>
- <SectionTitleLabel
- color="local"
- onClick={() => null} // added to test focus of label
- >
- With label
- </SectionTitleLabel>
+ <SectionTitleLabel color="local">With label</SectionTitleLabel>
</SectionTitle>
</Wrap>
<Wrap>
diff --git a/packages/design-system/src/components/section-title.tsx b/packages/design-system/src/components/section-title.tsx
index fbb318538a..d14cd14c8e 100644
--- a/packages/design-system/src/components/section-title.tsx
+++ b/packages/design-system/src/components/section-title.tsx
@@ -13,25 +13,34 @@ import {
createContext,
useContext,
} from "react";
-import { theme, css, type CSS } from "../stitches.config";
+import { theme, css, styled, type CSS } from "../stitches.config";
import { Button } from "./button";
import { cssVars } from "@webstudio-is/css-vars";
import { ArrowFocus } from "./primitives/arrow-focus";
-import { Label } from "./label";
+import { Label, isLabelButton } from "./label";
const buttonContentColor = cssVars.define("button-content-color");
const labelTextColor = cssVars.define("label-text-color");
+const StyledButton = styled(Button, {});
+
const containerStyle = css({
position: "relative",
height: theme.spacing[15],
[buttonContentColor]: theme.colors.foregroundSubtle,
+ [labelTextColor]: theme.colors.foregroundSubtle,
+
"&:hover, &:has(:focus-visible), &[data-state=open]": {
[buttonContentColor]: theme.colors.foregroundIconMain,
+ [labelTextColor]: theme.colors.foregroundMain,
+ },
+ // Remove hover style from the label when the button is hovered
+ [`&:hover:where(:has(${StyledButton}:hover))`]: {
+ [labelTextColor]: theme.colors.foregroundSubtle,
},
});
-const titleButtonStyle = css({
+const titleButtonLayoutStyle = css({
all: "unset", // reset <button>
display: "flex",
gap: theme.spacing[5],
@@ -41,11 +50,21 @@ const titleButtonStyle = css({
boxSizing: "border-box",
paddingLeft: theme.spacing[9],
paddingRight: theme.spacing[7],
- [labelTextColor]: theme.colors.foregroundSubtle,
- cursor: "pointer",
- "&:hover, &:focus-visible, &[data-state=open]": {
- [labelTextColor]: theme.colors.foregroundMain,
+ variants: {
+ // We assume that suffix is a <Button prefix={<Icon />} />
+ // (hard to support arbitrary width suffixes here, hopefully we'll never need to)
+ hasSuffix: { true: { paddingRight: theme.spacing[16] } },
},
+});
+
+const labelContainerStyle = css({
+ position: "absolute",
+ inset: 0,
+ pointerEvents: "none",
+});
+
+const titleButtonStyle = css(titleButtonLayoutStyle, {
+ cursor: "pointer",
"&:focus-visible::before": {
content: "''",
position: "absolute",
@@ -56,11 +75,6 @@ const titleButtonStyle = css({
borderRadius: theme.borderRadius[4],
border: `2px solid ${theme.colors.borderFocus}`,
},
- variants: {
- // We assume that suffix is a <Button prefix={<Icon />} />
- // (hard to support arbitrary width suffixes here, hopefully we'll never need to)
- hasSuffix: { true: { paddingRight: theme.spacing[16] } },
- },
});
const suffixSlotStyle = css({
@@ -128,16 +142,30 @@ export const SectionTitle = forwardRef(
data-state={state}
ref={ref}
{...props}
- >
- {children}
- {finalDots.length > 0 && (
- <div className={dotsSlotStyle()}>
- {finalDots.map((color) => (
- <div key={color} className={dotStyle({ color })} />
- ))}
- </div>
- )}
- </button>
+ ></button>
+
+ {/*
+ If the label is itself a button, we don't want to nest a button inside another button.
+ Therefore, we render the label in a layer above the SectionTitle button
+ */}
+ <div className={labelContainerStyle()}>
+ <div
+ className={titleButtonLayoutStyle({
+ hasSuffix: suffix !== undefined,
+ })}
+ >
+ {children}
+
+ {finalDots.length > 0 && (
+ <div className={dotsSlotStyle()}>
+ {finalDots.map((color) => (
+ <div key={color} className={dotStyle({ color })} />
+ ))}
+ </div>
+ )}
+ </div>
+ </div>
+
{suffix && <div className={suffixSlotStyle()}>{suffix}</div>}
</div>
)}
@@ -161,41 +189,25 @@ export const SectionTitleLabel = forwardRef(
const commonCss = { flex: "0 1 auto" };
- // When section is closed Label basically turns into an innert text
- // https://github.com/webstudio-is/webstudio-builder/issues/1271#issuecomment-1478262629
- if (state === "closed") {
- return (
- <Label
- truncate
- sectionTitle
- color="default"
- css={{
- cursor: "inherit",
- color: cssVars.use(labelTextColor),
- ...commonCss,
- ...css,
- }}
- ref={ref}
- >
- {children}
- </Label>
- );
- }
-
- const hasAction = Object.keys(props).some((prop) => prop.startsWith("on"));
+ const color = state === "closed" ? "default" : props.color;
+
+ const isButton = isLabelButton(color);
return (
<Label
truncate
sectionTitle
- tabIndex={hasAction ? -1 : undefined}
{...props}
+ color={color}
css={{
- // if there's no action, use cursor of SectionTitle,
- // otherwise use cursor of Label
- cursor: hasAction ? undefined : "inherit",
+ color: state === "closed" ? cssVars.use(labelTextColor) : undefined,
...commonCss,
...css,
+ // When we use a SectionTitle button, we can't directly render a label inside it.
+ // Instead, we need to render the label using a div that has position:absolute and pointer-events:none
+ // However, if the label itself is a button, we need to make sure that it remains clickable.
+ // @todo: move this logic to css
+ pointerEvents: isButton ? "all" : "inherit",
}}
ref={ref}
>
@@ -211,7 +223,7 @@ export const SectionTitleButton = forwardRef(
{ css, ...props }: ComponentProps<typeof Button>,
ref: Ref<HTMLButtonElement>
) => (
- <Button
+ <StyledButton
tabIndex={-1}
color="ghost"
{...props}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9c8f95b9bd..2ed4ccd25e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -312,7 +312,7 @@ importers:
version: 1.12.0(@remix-run/[email protected])
'@storybook/react':
specifier: ^6.5.16
'@types/css-tree':
specifier: ^2.0.0
version: 2.0.0
@@ -388,6 +388,9 @@ importers:
relative-deps:
specifier: ^1.0.7
version: 1.0.7
+ type-fest:
+ specifier: ^3.7.1
+ version: 3.7.1
typescript:
specifier: 4.9.5
version: 4.9.5
@@ -5218,6 +5221,46 @@ packages:
json-parse-even-better-errors: 2.3.1
dev: true
+ resolution: {integrity: sha512-bcKCAzF0DV2IIROp9ZHkRJa6O4jy7NlnHdWL3GmcUxYWNjLXkK5kfELELwEfSP5hXPfVL/qOGMAROuMQb9GG8Q==}
+ engines: {node: '>= 10.13'}
+ peerDependencies:
+ '@types/webpack': 4.x || 5.x
+ react-refresh: '>=0.10.0 <1.0.0'
+ sockjs-client: ^1.4.0
+ type-fest: '>=0.17.0 <3.0.0'
+ webpack: '>=4.43.0 <6.0.0'
+ webpack-dev-server: 3.x || 4.x
+ webpack-hot-middleware: 2.x
+ webpack-plugin-serve: 0.x || 1.x
+ peerDependenciesMeta:
+ '@types/webpack':
+ optional: true
+ sockjs-client:
+ optional: true
+ type-fest:
+ optional: true
+ webpack-dev-server:
+ optional: true
+ webpack-hot-middleware:
+ optional: true
+ webpack-plugin-serve:
+ optional: true
+ dependencies:
+ ansi-html-community: 0.0.8
+ common-path-prefix: 3.0.0
+ core-js-pure: 3.23.3
+ error-stack-parser: 2.1.4
+ find-up: 5.0.0
+ html-entities: 2.3.3
+ loader-utils: 2.0.4
+ react-refresh: 0.11.0
+ schema-utils: 3.1.1
+ source-map: 0.7.4
+ type-fest: 3.7.1
+ webpack: 4.46.0
+ dev: true
+
resolution: {integrity: sha512-bcKCAzF0DV2IIROp9ZHkRJa6O4jy7NlnHdWL3GmcUxYWNjLXkK5kfELELwEfSP5hXPfVL/qOGMAROuMQb9GG8Q==}
engines: {node: '>= 10.13'}
@@ -7932,6 +7975,93 @@ packages:
- supports-color
dev: true
+ resolution: {integrity: sha512-cBtNlOzf/MySpNLBK22lJ8wFU22HnfTB2xJyBk7W7Zi71Lm7Uxkhv1Pz8HdiQndJ0SlsAAQOWjQYsSZsGkZIaA==}
+ engines: {node: '>=10.13.0'}
+ hasBin: true
+ peerDependencies:
+ '@babel/core': ^7.11.5
+ '@storybook/builder-webpack4': '*'
+ '@storybook/builder-webpack5': '*'
+ '@storybook/manager-webpack4': '*'
+ '@storybook/manager-webpack5': '*'
+ react: ^16.8.0 || ^17.0.0 || ^18.0.0
+ react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0
+ require-from-string: ^2.0.2
+ typescript: '*'
+ peerDependenciesMeta:
+ '@babel/core':
+ optional: true
+ '@storybook/builder-webpack4':
+ optional: true
+ '@storybook/builder-webpack5':
+ optional: true
+ '@storybook/manager-webpack4':
+ optional: true
+ '@storybook/manager-webpack5':
+ optional: true
+ typescript:
+ optional: true
+ dependencies:
+ '@babel/core': 7.21.0
+ '@babel/preset-flow': 7.18.6(@babel/[email protected])
+ '@babel/preset-react': 7.18.6(@babel/[email protected])
+ '@pmmmwh/react-refresh-webpack-plugin': 0.5.7([email protected])([email protected])([email protected])
+ '@storybook/addons': 6.5.16([email protected])([email protected])
+ '@storybook/client-logger': 6.5.16
+ '@storybook/core-common': 6.5.16([email protected])([email protected])([email protected])
+ '@storybook/csf': 0.0.2--canary.4566f4d.1
+ '@storybook/docs-tools': 6.5.16([email protected])([email protected])
+ '@storybook/node-logger': 6.5.16
+ '@storybook/react-docgen-typescript-plugin': 1.0.2-canary.6.9d540b91e815f8fc2f8829189deb00553559ff63.0([email protected])([email protected])
+ '@storybook/semver': 7.3.2
+ '@storybook/store': 6.5.16([email protected])([email protected])
+ '@types/estree': 0.0.51
+ '@types/node': 16.18.11
+ '@types/webpack-env': 1.18.0
+ acorn: 7.4.1
+ acorn-jsx: 5.3.2([email protected])
+ acorn-walk: 7.2.0
+ babel-plugin-add-react-displayname: 0.0.5
+ babel-plugin-react-docgen: 4.2.1
+ core-js: 3.23.3
+ escodegen: 2.0.0
+ fs-extra: 9.1.0
+ global: 4.4.0
+ html-tags: 3.2.0
+ lodash: 4.17.21
+ prop-types: 15.8.1
+ react: 17.0.2
+ react-dom: 17.0.2([email protected])
+ react-element-to-jsx-string: 14.3.4([email protected])([email protected])
+ react-refresh: 0.11.0
+ read-pkg-up: 7.0.1
+ regenerator-runtime: 0.13.11
+ require-from-string: 2.0.2
+ ts-dedent: 2.2.0
+ typescript: 4.9.5
+ util-deprecate: 1.0.2
+ webpack: 4.46.0
+ transitivePeerDependencies:
+ - '@storybook/mdx2-csf'
+ - '@types/webpack'
+ - bluebird
+ - bufferutil
+ - encoding
+ - eslint
+ - sockjs-client
+ - supports-color
+ - type-fest
+ - utf-8-validate
+ - vue-template-compiler
+ - webpack-cli
+ - webpack-command
+ - webpack-dev-server
+ - webpack-hot-middleware
+ - webpack-plugin-serve
+ dev: true
+
resolution: {integrity: sha512-cBtNlOzf/MySpNLBK22lJ8wFU22HnfTB2xJyBk7W7Zi71Lm7Uxkhv1Pz8HdiQndJ0SlsAAQOWjQYsSZsGkZIaA==}
engines: {node: '>=10.13.0'}
@@ -19510,6 +19640,11 @@ packages:
resolution: {integrity: sha512-RAH822pAdBgcNMAfWnCBU3CFZcfZ/i1eZjwFU/dsLKumyuuP3niueg2UAukXYF0E2AAoc82ZSSf9J0WQBinzHA==}
engines: {node: '>=12.20'}
+ resolution: {integrity: sha512-8LZNdvuztgxCF4eYpEmPYUPS0lbbByM2qHcp2oMxHZhWLIQB9QE36EeQ1PKwsUIDZXEP8HCBEmkBbT1//kLU4Q==}
+ engines: {node: '>=14.16'}
+ dev: true
+
resolution: {integrity: sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==}
engines: {node: '>= 0.6'}