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 flex grid control states (#682)

Bogdan Chadkine7af8d0a6ebc
diff --git a/apps/designer/.storybook/preview.tsx b/apps/designer/.storybook/preview.tsx
index d4d4c2037d..069dc2f011 100644
--- a/apps/designer/.storybook/preview.tsx
+++ b/apps/designer/.storybook/preview.tsx
@@ -1,6 +1,6 @@
import * as React from "react";
import { TooltipProvider } from "@radix-ui/react-tooltip";
-import { darkTheme, globalCss } from "@webstudio-is/design-system";
+import { globalCss } from "@webstudio-is/design-system";
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
@@ -22,7 +22,6 @@ const globalStyles = globalCss({
export const decorators = [
(StoryFn: any) => {
- document.body.classList.add(darkTheme);
globalStyles();
return (
<TooltipProvider>
diff --git a/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.stories.tsx b/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.stories.tsx
new file mode 100644
index 0000000000..a87fe99caf
--- /dev/null
+++ b/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.stories.tsx
@@ -0,0 +1,82 @@
+import { Box, Flex } from "@webstudio-is/design-system";
+import { FlexGrid } from "./flex-grid";
+
+export default {
+ component: FlexGrid,
+};
+
+const batchUpdate = {
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
+ setProperty: () => () => {},
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
+ deleteProperty: () => {},
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
+ publish: () => {},
+};
+
+const alignItems = ["normal", "stretch", "baseline", "center", "start", "end"];
+const justifyContent = [
+ "normal",
+ "space-between",
+ "space-around",
+ "center",
+ "start",
+ "end",
+];
+
+const Base = ({ flexDirection }: { flexDirection: string }) => {
+ return (
+ <Flex direction="column" gap={2} css={{ color: "#000" }}>
+ <Flex gap={2}>
+ <Box css={{ fontSize: 10, writingMode: "vertical-lr" }}>&nbsp;</Box>
+ {justifyContent.map((justifyContent) => (
+ <Box key={justifyContent} css={{ width: 72, fontSize: 10 }}>
+ {justifyContent}
+ </Box>
+ ))}
+ </Flex>
+ {alignItems.map((alignItems) => (
+ <Flex key={alignItems} gap={2}>
+ <Box
+ css={{
+ fontSize: 10,
+ textAlign: "right",
+ writingMode: "vertical-lr",
+ transform: "rotate(180deg)",
+ }}
+ >
+ {alignItems}
+ </Box>
+ {justifyContent.map((justifyContent) => (
+ <Box key={justifyContent} css={{ width: 72, height: 72 }}>
+ <FlexGrid
+ currentStyle={{
+ flexDirection: { type: "keyword", value: flexDirection },
+ justifyContent: { type: "keyword", value: justifyContent },
+ alignItems: { type: "keyword", value: alignItems },
+ }}
+ batchUpdate={batchUpdate}
+ />
+ </Box>
+ ))}
+ </Flex>
+ ))}
+ </Flex>
+ );
+};
+
+export const Row = () => {
+ return <Base flexDirection="row" />;
+};
+
+export const RowReverse = () => {
+ return <Base flexDirection="row-reverse" />;
+};
+
+export const Column = () => {
+ return <Base flexDirection="column" />;
+};
+
+export const ColumnReverse = () => {
+ return <Base flexDirection="column-reverse" />;
+};
diff --git a/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.tsx b/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.tsx
index 6fa36944ef..47e87143a9 100644
--- a/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.tsx
+++ b/apps/designer/app/designer/features/style-panel/sections/layout/shared/flex-grid.tsx
@@ -1,8 +1,13 @@
-import { Flex, Grid, DeprecatedIconButton } from "@webstudio-is/design-system";
+import {
+ Box,
+ Flex,
+ Grid,
+ DeprecatedIconButton,
+} from "@webstudio-is/design-system";
+import type { Style } from "@webstudio-is/css-data";
import { toValue } from "@webstudio-is/css-engine";
import { DotFilledIcon } from "@webstudio-is/icons";
import { useIsFromCurrentBreakpoint } from "../../../shared/use-is-from-current-breakpoint";
-import type { Style } from "@webstudio-is/css-data";
import type { CreateBatchUpdate } from "../../../shared/use-style-data";
export const FlexGrid = ({
@@ -22,37 +27,13 @@ export const FlexGrid = ({
const flexDirection = toValue(currentStyle.flexDirection);
const justifyContent = toValue(currentStyle.justifyContent);
const alignItems = toValue(currentStyle.alignItems);
- const direction = Number(flexDirection.includes("column"));
- const cells = [
- "_0_0",
- "_0_1",
- "_0_2",
- "_1_0",
- "_1_1",
- "_1_2",
- "_2_0",
- "_2_1",
- "_2_2",
- ];
- const rowOptions: Record<string, number> = {
- stretch: 0,
- normal: 0,
- start: 0,
- center: 1,
- end: 2,
- };
- const columnOptions: Record<string, number> = {
- "space-around": 0,
- normal: 0,
- start: 0,
- center: 1,
- end: 2,
- };
- const rowValue = rowOptions[alignItems];
- const columnValue = columnOptions[justifyContent];
- const currentPosition = rowValue * 3 + columnValue;
const setAlignItems = batchUpdate.setProperty("alignItems");
const setJustifyContent = batchUpdate.setProperty("justifyContent");
+ const alignment = ["start", "center", "end"];
+ const gridSize = alignment.length;
+ const isFlexDirectionColumn =
+ flexDirection === "column" || flexDirection === "column-reverse";
+
return (
<Grid
css={{
@@ -66,89 +47,85 @@ export const FlexGrid = ({
gap: "$spacing$1",
gridTemplateColumns: "repeat(3, 1fr)",
gridTemplateRows: "repeat(3, 1fr)",
- gridTemplateAreas: `
- "${cells.slice(0, 0 + 3).join(" ")}"
- "${cells.slice(3, 3 + 3).join(" ")}"
- "${cells.slice(6, 6 + 3).join(" ")}"
- `,
color: isCurrentBreakpoint ? "$colors$blue9" : "$colors$slate8",
- transform: direction
- ? "rotate(-90deg) scaleX(-1)"
- : "rotate(0deg) scaleX(1)",
}}
>
- {cells.map((value, index) => (
- <Flex
- justify="center"
- align="center"
- key={index}
- css={{
- gridArea: value,
- width: "100%",
- height: "100%",
- }}
- >
- <DeprecatedIconButton
+ {Array.from(Array(gridSize * gridSize), (_, index) => {
+ const x = index % gridSize;
+ const y = Math.floor(index / gridSize);
+ // grid edges starts with 1
+ let gridColumn = `${x + 1} / ${x + 2}`;
+ let gridRow = `${y + 1} / ${y + 2}`;
+ if (isFlexDirectionColumn) {
+ [gridColumn, gridRow] = [gridRow, gridColumn];
+ }
+ return (
+ <Flex
+ key={index}
+ justify="center"
+ align="center"
css={{
width: "100%",
height: "100%",
- color: "$colors$gray8",
- "&:hover": {
- bc: "$colors$slate4",
- },
- "&:focus": {
- background: "none",
- boxShadow: "none",
- outline: "none",
- },
- }}
- onClick={() => {
- const [alignItems, justifyContent] = cells[index]
- .slice(1)
- .split("_")
- .map((value) => ["start", "center", "end"][parseFloat(value)]);
- setAlignItems(alignItems);
- setJustifyContent(justifyContent);
- batchUpdate.publish();
+ gridColumn,
+ gridRow,
}}
>
- <DotFilledIcon />
- </DeprecatedIconButton>
- </Flex>
- ))}
+ <DeprecatedIconButton
+ css={{
+ width: "100%",
+ height: "100%",
+ color: "$colors$gray8",
+ "&:hover": {
+ bc: "$colors$slate4",
+ },
+ "&:focus": {
+ background: "none",
+ boxShadow: "none",
+ outline: "none",
+ },
+ }}
+ onClick={() => {
+ const justifyContent = alignment[x];
+ const alignItems = alignment[y];
+ setAlignItems(alignItems);
+ setJustifyContent(justifyContent);
+ batchUpdate.publish();
+ }}
+ >
+ <DotFilledIcon />
+ </DeprecatedIconButton>
+ </Flex>
+ );
+ })}
+
<Flex
css={{
- gridArea: cells[currentPosition],
- ...(alignItems === "stretch" && {
- gridColumnEnd: "none",
- }),
- ...(justifyContent === "space-around" && {
- gridRowEnd: "none",
- justifyContent: "space-around",
- }),
- alignItems:
- `start center end start center end start center end`.split(" ")[
- currentPosition
- ],
- pointerEvents: "none",
- flexDirection: "column",
width: "100%",
height: "100%",
- gap: "calc($spacing$3 / 2)",
- color: "currentColor",
+ // fill whole grid
+ gridColumn: "-1 / 1",
+ gridRow: "-1 / 1",
+ p: 1,
+ gap: 2,
+ pointerEvents: "none",
+ // controlled styles
+ flexDirection,
+ justifyContent,
+ alignItems,
}}
>
- {["60%", "100%", "60%"].map((value, index) => (
- <Flex
- key={index}
+ {[10, 16, 8].map((size) => (
+ <Box
+ key={size}
css={{
- inlineSize: value,
- blockSize: "calc(100% / 3)",
- maxBlockSize: "$spacing$4",
- background: "currentColor",
borderRadius: "calc($borderRadius$4 / 2)",
+ backgroundColor: "currentColor",
+ ...(isFlexDirectionColumn
+ ? { minWidth: size, minHeight: 4 }
+ : { minWidth: 4, minHeight: size }),
}}
- ></Flex>
+ ></Box>
))}
</Flex>
</Grid>