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: rewrite radix navigation menu template with jsx (#4702)
Bogdan Chadkinbe69438303ac
diff --git a/packages/sdk-components-react-radix/src/navigation-menu.template.tsx b/packages/sdk-components-react-radix/src/navigation-menu.template.tsxnew file mode 100644index 0000000000..005862303b--- /dev/null+++ b/packages/sdk-components-react-radix/src/navigation-menu.template.tsx@@ -0,0 +1,323 @@+import type { ReactNode } from "react";+import { ChevronDownIcon } from "@webstudio-is/icons/svg";+import {+ $,+ css,+ PlaceholderValue,+ type TemplateMeta,+} from "@webstudio-is/template";+import { radix } from "./shared/proxy";+import { getButtonStyle } from "./shared/styles";+import {+ borderRadius,+ borderWidth,+ boxShadow,+ colors,+ fontSize,+ height,+ lineHeight,+ spacing,+ transition,+ weights,+ width,+} from "./shared/theme";++const components = [+ {+ title: "Sheet",+ href: "/docs/components/sheet",+ description:+ "Extends the Dialog component to display content that complements the main content of the screen.",+ },+ {+ title: "Navigation Menu",+ href: "/docs/components/navigation-menu",+ description: "A collection of links for navigating websites.",+ },+ {+ title: "Tabs",+ href: "/docs/components/tabs",+ description:+ "A set of layered sections of content—known as tab panels—that are displayed one at a time.",+ },+ {+ title: "Accordion",+ href: "/docs/components/accordion",+ description:+ "A vertically stacked set of interactive headings that each reveal a section of content.",+ },+ {+ title: "Dialog",+ href: "/docs/components/dialog",+ description:+ "A window overlaid on either the primary window or another dialog window, rendering the content underneath inert.",+ },+ {+ title: "Collapsible",+ href: "/docs/components/collapsible",+ description: "An interactive component which expands/collapses a panel.",+ },+ {+ title: "Popover",+ href: "/docs/components/popover",+ description: "Displays rich content in a portal, triggered by a button.",+ },+ {+ title: "Tooltip",+ href: "/docs/components/tooltip",+ description:+ "A popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it.",+ },+ {+ title: "Button",+ href: "/docs/components/button",+ description: "Displays a button or a component that looks like a button.",+ },+];++const createMenuContentItem = (props: (typeof components)[number]) => (+ <radix.NavigationMenuLink>+ <$.Link+ href={`https://ui.shadcn.com${props.href}`}+ // block select-none space-y-1 rounded-md p-3 leading-none+ // no-underline outline-none transition-colors+ // hover:bg-accent hover:text-accent-foreground+ // focus:bg-accent focus:text-accent-foreground+ ws:style={css`+ color: inherit;+ display: flex;+ flex-direction: column;+ user-select: none;+ gap: ${spacing[1]};+ border-radius: ${borderRadius.md};+ padding: ${spacing[3]};+ line-height: ${lineHeight.none};+ text-decoration-line: none;+ outline: none;+ &:hover,+ &:focus {+ background-color: ${colors.accent};+ color: ${colors.accentForeground};+ }+ `}+ >+ <$.Text+ // text-sm font-medium leading-none+ ws:style={css`+ font-size: ${fontSize.sm};+ font-weight: ${weights.medium};+ line-height: ${lineHeight.none};+ `}+ >+ {new PlaceholderValue(props.title)}+ </$.Text>+ <$.Paragraph+ // line-clamp-2 text-sm leading-snug text-muted-foreground+ ws:style={css`+ margin: 0;+ overflow: hidden;+ display: -webkit-box;+ -webkit-box-orient: vertical;+ -webkit-line-clamp: 2;+ font-size: ${fontSize.sm};+ line-height: ${lineHeight.snug};+ color: ${colors.mutedForeground};+ `}+ >+ {new PlaceholderValue(props.description)}+ </$.Paragraph>+ </$.Link>+ </radix.NavigationMenuLink>+);++const createMenuContentList = (props: { count: number; offset: number }) => (+ <$.Box+ ws:label="Flex Column"+ ws:style={css`+ width: ${width[64]};+ display: flex;+ gap: ${spacing[4]};+ flex-direction: column;+ `}+ >+ {components+ .slice(props.offset, props.offset + props.count)+ .map(createMenuContentItem)}+ </$.Box>+);++const aboutMenuContent = (+ <$.Box+ ws:label="Content"+ ws:style={css`+ display: flex;+ gap: ${spacing[4]};+ padding: ${spacing[2]};+ `}+ >+ <$.Box+ ws:style={css`+ background-color: ${colors.border};+ padding: ${spacing[4]};+ width: ${width[48]};+ border-radius: ${borderRadius.md};+ `}+ >+ {new PlaceholderValue("")}+ </$.Box>+ {createMenuContentList({ count: 3, offset: 0 })}+ </$.Box>+);++const componentsMenuContent = (+ <$.Box+ ws:label="Content"+ ws:style={css`+ display: flex;+ gap: ${spacing[4]};+ `}+ >+ {createMenuContentList({ count: 3, offset: 3 })}+ {createMenuContentList({ count: 3, offset: 6 })}+ </$.Box>+);++const createMenuItem = (title: string, content: ReactNode) => {+ return (+ <radix.NavigationMenuItem>+ <radix.NavigationMenuTrigger>+ <$.Button+ ws:style={[+ ...getButtonStyle("ghost", "sm"),+ ...css`+ --navigation-menu-trigger-icon-transform: 0deg;+ &[data-state="open"] {+ --navigation-menu-trigger-icon-transform: 180deg;+ }+ `,+ ]}+ >+ <$.Text>{new PlaceholderValue(title)}</$.Text>+ <$.Box+ ws:label="Icon Container"+ // h-4 w-4 shrink-0 transition-transform duration-200+ ws:style={css`+ margin-left: ${spacing[1]};+ rotate: var(--navigation-menu-trigger-icon-transform);+ height: ${height[4]};+ width: ${width[4]};+ flex-shrink: 0;+ transition: ${transition.all};+ transition-duration: 200ms;+ `}+ >+ <$.HtmlEmbed ws:label="Chevron Icon" code={ChevronDownIcon} />+ </$.Box>+ </$.Button>+ </radix.NavigationMenuTrigger>+ <radix.NavigationMenuContent+ // left-0 top-0 absolute w-max+ ws:style={css`+ left: 0;+ top: 0;+ positon: absolute;+ width: max-content;+ padding: ${spacing[4]};+ `}+ >+ {content}+ </radix.NavigationMenuContent>+ </radix.NavigationMenuItem>+ );+};++const createMenuLink = (title: string) => {+ return (+ <radix.NavigationMenuItem>+ <radix.NavigationMenuLink>+ <$.Link+ ws:style={[+ ...getButtonStyle("ghost", "sm"),+ ...css`+ text-decoration-line: none;+ color: currentColor;+ `,+ ]}+ >+ {new PlaceholderValue(title)}+ </$.Link>+ </radix.NavigationMenuLink>+ </radix.NavigationMenuItem>+ );+};++export const meta: TemplateMeta = {+ category: "radix",+ description: "A collection of links for navigating websites.",+ order: 2,+ template: (+ <radix.NavigationMenu+ // relative+ // Omiting this: z-10 flex max-w-max flex-1 items-center justify-center+ ws:style={css`+ position: relative;+ max-width: max-content;+ `}+ >+ <radix.NavigationMenuList+ ws:style={css`+ /* ul defaults in tailwind */+ padding: 0;+ margin: 0;+ /* shadcdn styles */+ display: flex;+ flex: 1 1 0;+ list-style-type: none;+ align-items: center;+ justify-content: center;+ gap: ${spacing[1]};+ `}+ >+ {createMenuItem("About", aboutMenuContent)}+ {createMenuItem("Components", componentsMenuContent)}+ {createMenuLink("Standalone")}+ </radix.NavigationMenuList>+ <$.Box+ ws:label="Viewport Container"+ // absolute left-0 top-full flex justify-center+ ws:style={css`+ position: absolute;+ left: 0;+ top: 100%;+ display: flex;+ justify-content: center;+ `}+ >+ <radix.NavigationMenuViewport+ /*+ origin-top-center relative mt-1.5 w-full+ overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-lg+ h-[var(--radix-navigation-menu-viewport-height)]+ w-[var(--radix-navigation-menu-viewport-width)]+ // anims+ [animation-duration:150ms!important] [transition-duration:150ms!important]+ data-[state=open]:animate-in data-[state=closed]:animate-out+ data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-90+ */+ ws:style={css`+ position: relative;+ margin-top: ${spacing[1.5]};+ overflow: hidden;+ border-radius: ${borderRadius.md};+ border: ${borderWidth.DEFAULT} solid ${colors.border};+ background-color: ${colors.popover};+ color: ${colors.popoverForeground};+ box-shadow: ${boxShadow.lg};+ height: var(--radix-navigation-menu-viewport-height);+ width: var(--radix-navigation-menu-viewport-width);+ `}+ />+ </$.Box>+ </radix.NavigationMenu>+ ),+};diff --git a/packages/sdk-components-react-radix/src/navigation-menu.ws.ts b/packages/sdk-components-react-radix/src/navigation-menu.ws.tsindex cc040455d7..49f836a26e 100644--- a/packages/sdk-components-react-radix/src/navigation-menu.ws.ts+++ b/packages/sdk-components-react-radix/src/navigation-menu.ws.ts@@ -4,7 +4,6 @@ import {ListItemIcon,TriggerIcon,BoxIcon,- ChevronDownIcon,ViewportIcon,NavigationMenuIcon,} from "@webstudio-is/icons/svg";@@ -14,8 +13,6 @@ import type {WsComponentPropsMeta,} from "@webstudio-is/react-sdk";import { div } from "@webstudio-is/sdk/normalize.css";-import * as tc from "./theme/tailwind-classes";-import { getButtonStyles } from "./theme/styles";import {propsNavigationMenu,propsNavigationMenuItem,@@ -30,287 +27,8 @@ const presetStyle = {div,} satisfies PresetStyle<"div">;-const components = [- {- title: "Sheet",- href: "/docs/components/sheet",- description:- "Extends the Dialog component to display content that complements the main content of the screen.",- },- {- title: "Navigation Menu",- href: "/docs/components/navigation-menu",- description: "A collection of links for navigating websites.",- },- {- title: "Tabs",- href: "/docs/components/tabs",- description:- "A set of layered sections of content—known as tab panels—that are displayed one at a time.",- },- {- title: "Accordion",- href: "/docs/components/accordion",- description:- "A vertically stacked set of interactive headings that each reveal a section of content.",- },- {- title: "Dialog",- href: "/docs/components/dialog",- description:- "A window overlaid on either the primary window or another dialog window, rendering the content underneath inert.",- },- {- title: "Collapsible",- href: "/docs/components/collapsible",- description: "An interactive component which expands/collapses a panel.",- },-- {- title: "Popover",- href: "/docs/components/popover",- description: "Displays rich content in a portal, triggered by a button.",- },-- {- title: "Tooltip",- href: "/docs/components/tooltip",- description:- "A popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it.",- },-- {- title: "Button",- href: "/docs/components/button",- description: "Displays a button or a component that looks like a button.",- },-];--const navItem = (- props: (typeof components)[number]-): NonNullable<WsComponentMeta["template"]> => [- {- type: "instance",- component: "NavigationMenuLink",- children: [- {- type: "instance",- component: "Link",- // block select-none space-y-1 rounded-md p-3 leading-none- // no-underline outline-none transition-colors- // hover:bg-accent hover:text-accent-foreground- // focus:bg-accent focus:text-accent-foreground- styles: [- tc.text("inherit"),- tc.flex(),- tc.flex("col"),- tc.select("none"),- tc.gap(1),- tc.rounded("md"),- tc.p(3),- tc.leading("none"),- tc.noUnderline(),- tc.outline("none"),- tc.hover([tc.bg("accent"), tc.text("accentForeground")].flat()),- tc.focus([tc.bg("accent"), tc.text("accentForeground")].flat()),- ].flat(),- props: [- {- name: "href",- type: "string",- value: `https://ui.shadcn.com${props.href}`,- },- ],- children: [- {- type: "instance",- component: "Text",- // text-sm font-medium leading-none- styles: [- tc.text("sm"),- tc.font("medium"),- tc.leading("none"),- ].flat(),- children: [- {- type: "text",- value: props.title,- placeholder: true,- },- ],- },- {- type: "instance",- component: "Paragraph",- // line-clamp-2 text-sm leading-snug text-muted-foreground- styles: [- tc.m(0),- tc.lineClamp(2),- tc.text("sm"),- tc.leading("snug"),- tc.text("mutedForeground"),- ].flat(),-- children: [- {- type: "text",- value: props.description,- placeholder: true,- },- ],- },- ],- },- ],- },-];--const navItemsList = (props: {- count: number;- offset: number;-}): NonNullable<WsComponentMeta["template"]> => [- {- type: "instance",- component: "Box",- label: "Flex Column",- styles: [tc.w(64), tc.flex(), tc.gap(4), tc.flex("col")].flat(),- children: Array.from(Array(props.count), (_, index) =>- navItem(components[index + props.offset])- ).flat(),- },-];--const menuItemLink = (props: {- title: string;-}): NonNullable<WsComponentMeta["template"]> => [- {- type: "instance",- component: "NavigationMenuItem",- children: [- {- type: "instance",- component: "NavigationMenuLink",- children: [- {- type: "instance",- component: "Link",- styles: [- getButtonStyles("ghost", "sm"),- tc.noUnderline(),- tc.text("current"),- ].flat(),- children: [{ type: "text", value: props.title, placeholder: true }],- },- ],- },- ],- },-];--const menuItem = (props: {- title: string;- children: NonNullable<WsComponentMeta["template"]>;- padding: 0 | 2;-}): NonNullable<WsComponentMeta["template"]> => [- {- type: "instance",- component: "NavigationMenuItem",- children: [- {- type: "instance",- component: "NavigationMenuTrigger",- children: [- {- type: "instance",- component: "Button",- styles: [- getButtonStyles("ghost", "sm"),- tc.property("--navigation-menu-trigger-icon-transform", "0deg"),- tc.state(- [- tc.property(- "--navigation-menu-trigger-icon-transform",- "180deg"- ),- ],- "[data-state=open]"- ),- ].flat(),- children: [- {- type: "instance",- component: "Text",- children: [- { type: "text", value: props.title, placeholder: true },- ],- },- {- type: "instance",- component: "Box",- label: "Icon Container",- // h-4 w-4 shrink-0 transition-transform duration-200- styles: [- tc.ml(1),- tc.property(- "rotate",- "--navigation-menu-trigger-icon-transform"- ),- tc.h(4),- tc.w(4),- tc.shrink(0),- tc.transition("all"),- tc.duration(200),- ].flat(),- children: [- {- type: "instance",- component: "HtmlEmbed",- label: "Chevron Icon",- props: [- {- type: "string",- name: "code",- value: ChevronDownIcon,- },- ],- children: [],- },- ],- },- ],- },- ],- },- {- type: "instance",- component: "NavigationMenuContent",- // left-0 top-0 absolute w-max- styles: [- tc.left(0),- tc.top(0),- tc.absolute(),- tc.w("max"),- tc.p(4),- ].flat(),- children: [- {- type: "instance",- component: "Box",- label: "Content",- styles: [tc.flex(), tc.gap(4), tc.p(props.padding)].flat(),- children: props.children,- },- ],- },- ],- },-];-export const metaNavigationMenu: WsComponentMeta = {- category: "radix",- order: 2,type: "container",- description: "A collection of links for navigating websites.",icon: NavigationMenuIcon,presetStyle,constraints: [@@ -323,120 +41,9 @@ export const metaNavigationMenu: WsComponentMeta = {component: { $eq: "NavigationMenuViewport" },},],- template: [- {- type: "instance",- component: "NavigationMenu",- props: [],- // relative- // Omiting this: z-10 flex max-w-max flex-1 items-center justify-center- styles: [tc.relative(), tc.maxW("max")].flat(),- children: [- {- type: "instance",- component: "NavigationMenuList",- styles: [- // ul defaults in tailwind- tc.p(0),- tc.m(0),- // shadcdn styles- tc.flex(),- tc.flex(1),- tc.list("none"),- tc.items("center"),- tc.justify("center"),- tc.gap(1),- ].flat(),- children: [- ...menuItem({- title: "About",- padding: 2,- children: [- {- type: "instance",- component: "Box",- styles: [- tc.bg("border"),- tc.p(4),- tc.w(48),- tc.rounded("md"),- ].flat(),- children: [- {- type: "text",- value: "",- placeholder: true,- },- ],- },- ...navItemsList({ count: 3, offset: 0 }),- ],- }),- ...menuItem({- title: "Components",- padding: 0,- children: [- ...navItemsList({ count: 3, offset: 3 }),- ...navItemsList({ count: 3, offset: 6 }),- ],- }),- ...menuItemLink({ title: "Standalone" }),- ],- },-- {- type: "instance",- component: "Box",- label: "Viewport Container",- // absolute left-0 top-full flex justify-center- styles: [- tc.absolute(),- tc.left(0),- tc.top("full"),- tc.flex(),- tc.justify("center"),- ].flat(),-- children: [- {- type: "instance",- component: "NavigationMenuViewport",- /*- origin-top-center relative mt-1.5 w-full- overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-lg- h-[var(--radix-navigation-menu-viewport-height)]- w-[var(--radix-navigation-menu-viewport-width)]- // anims- [animation-duration:150ms!important] [transition-duration:150ms!important]- data-[state=open]:animate-in data-[state=closed]:animate-out- data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-90- */- styles: [- tc.relative(),- tc.mt(1.5),- tc.overflow("hidden"),- tc.rounded("md"),- tc.border(),- tc.bg("popover"),- tc.text("popoverForeground"),- tc.shadow("lg"),- tc.property(- "height",- "--radix-navigation-menu-viewport-height"- ),- tc.property("width", "--radix-navigation-menu-viewport-width"),- ].flat(),- children: [],- },- ],- },- ],- },- ],};export const metaNavigationMenuList: WsComponentMeta = {- category: "hidden",type: "container",icon: ListIcon,constraints: [@@ -454,7 +61,6 @@ export const metaNavigationMenuList: WsComponentMeta = {};export const metaNavigationMenuItem: WsComponentMeta = {- category: "hidden",type: "container",icon: ListItemIcon,constraints: {@@ -467,7 +73,6 @@ export const metaNavigationMenuItem: WsComponentMeta = {};export const metaNavigationMenuTrigger: WsComponentMeta = {- category: "hidden",stylable: false,type: "container",icon: TriggerIcon,@@ -480,7 +85,6 @@ export const metaNavigationMenuTrigger: WsComponentMeta = {};export const metaNavigationMenuContent: WsComponentMeta = {- category: "hidden",type: "container",icon: ContentIcon,constraints: {@@ -493,7 +97,6 @@ export const metaNavigationMenuContent: WsComponentMeta = {};export const metaNavigationMenuLink: WsComponentMeta = {- category: "hidden",type: "container",stylable: false,icon: BoxIcon,@@ -512,7 +115,6 @@ export const metaNavigationMenuLink: WsComponentMeta = {};export const metaNavigationMenuViewport: WsComponentMeta = {- category: "hidden",type: "container",icon: ViewportIcon,constraints: {diff --git a/packages/sdk-components-react-radix/src/shared/styles.ts b/packages/sdk-components-react-radix/src/shared/styles.tsindex 2b7c334011..c13cbb895c 100644--- a/packages/sdk-components-react-radix/src/shared/styles.ts+++ b/packages/sdk-components-react-radix/src/shared/styles.ts@@ -64,6 +64,13 @@ const defaultButtonStyle = css`padding: ${spacing[2]} ${spacing[4]};`;+// h-9 rounded-md px-3+const smButtonStyle = css`+ height: ${height[9]};+ border-radius: ${borderRadius.md};+ padding: 0 ${spacing[3]};+`;+// Set explicit paddings for IOS Safari to prevent the icon from collapsing// h-10 w-10const iconButtonStyle = css`@@ -74,7 +81,7 @@ const iconButtonStyle = css`export const getButtonStyle = (variant: "outline" | "ghost",- size: "default" | /*"sm" |*/ "icon" = "default"+ size: "default" | "sm" | "icon" = "default") => {const style: TemplateStyleDecl[] = [...buttonStyle];if (variant === "ghost") {@@ -86,6 +93,9 @@ export const getButtonStyle = (if (size === "default") {style.push(...defaultButtonStyle);}+ if (size === "sm") {+ style.push(...smButtonStyle);+ }if (size === "icon") {style.push(...iconButtonStyle);}diff --git a/packages/sdk-components-react-radix/src/templates.ts b/packages/sdk-components-react-radix/src/templates.tsindex 2500e1380f..1be856bca1 100644--- a/packages/sdk-components-react-radix/src/templates.ts+++ b/packages/sdk-components-react-radix/src/templates.ts@@ -10,3 +10,4 @@ export { meta as Tooltip } from "./tooltip.template";export { meta as Popover } from "./popover.template";export { meta as RadioGroup } from "./radio-group.template";export { meta as Select } from "./select.template";+export { meta as NavigationMenu } from "./navigation-menu.template";