|
| 1 | +import * as navigationMenu from "@zag-js/navigation-menu" |
| 2 | +import { normalizeProps, useMachine } from "@zag-js/react" |
| 3 | +import { navigationMenuControls } from "@zag-js/shared" |
| 4 | +import { ChevronDown } from "lucide-react" |
| 5 | +import { useId } from "react" |
| 6 | +import { Presence } from "../components/presence" |
| 7 | +import { StateVisualizer } from "../components/state-visualizer" |
| 8 | +import { Toolbar } from "../components/toolbar" |
| 9 | +import { useControls } from "../hooks/use-controls" |
| 10 | + |
| 11 | +export default function Page() { |
| 12 | + const controls = useControls(navigationMenuControls) |
| 13 | + |
| 14 | + const service = useMachine(navigationMenu.machine, { |
| 15 | + id: useId(), |
| 16 | + ...controls.context, |
| 17 | + }) |
| 18 | + |
| 19 | + const api = navigationMenu.connect(service, normalizeProps) |
| 20 | + |
| 21 | + const renderLinks = (opts: { value: string; items: string[] }) => { |
| 22 | + const { value, items } = opts |
| 23 | + return items.map((item, index) => ( |
| 24 | + <a href="#" key={`${value}-${item}-${index}`} {...api.getLinkProps({ value })}> |
| 25 | + {item} |
| 26 | + </a> |
| 27 | + )) |
| 28 | + } |
| 29 | + |
| 30 | + return ( |
| 31 | + <> |
| 32 | + <main className="navigation-menu viewport"> |
| 33 | + <Navbar> |
| 34 | + <div {...api.getRootProps()}> |
| 35 | + <div {...api.getIndicatorTrackProps()}> |
| 36 | + <div {...api.getListProps()}> |
| 37 | + <div {...api.getItemProps({ value: "products" })}> |
| 38 | + <button {...api.getTriggerProps({ value: "products" })}> |
| 39 | + Products |
| 40 | + <ChevronDown /> |
| 41 | + </button> |
| 42 | + </div> |
| 43 | + |
| 44 | + <div {...api.getItemProps({ value: "company" })}> |
| 45 | + <button {...api.getTriggerProps({ value: "company" })}> |
| 46 | + Company |
| 47 | + <ChevronDown /> |
| 48 | + </button> |
| 49 | + </div> |
| 50 | + |
| 51 | + <div {...api.getItemProps({ value: "developers", disabled: true })}> |
| 52 | + <button {...api.getTriggerProps({ value: "developers", disabled: true })}> |
| 53 | + Developers |
| 54 | + <ChevronDown /> |
| 55 | + </button> |
| 56 | + </div> |
| 57 | + |
| 58 | + <div {...api.getItemProps({ value: "pricing" })}> |
| 59 | + <a href="#" {...api.getLinkProps({ value: "pricing" })}> |
| 60 | + Pricing |
| 61 | + </a> |
| 62 | + </div> |
| 63 | + |
| 64 | + <Presence {...api.getIndicatorProps()}> |
| 65 | + <div {...api.getArrowProps()} /> |
| 66 | + </Presence> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + |
| 70 | + <div {...api.getViewportPositionerProps()}> |
| 71 | + <Presence {...api.getViewportProps()}> |
| 72 | + <Presence |
| 73 | + {...api.getContentProps({ value: "products" })} |
| 74 | + style={{ |
| 75 | + gridTemplateColumns: "1fr 2fr", |
| 76 | + width: 600, |
| 77 | + }} |
| 78 | + > |
| 79 | + {renderLinks({ |
| 80 | + value: "products", |
| 81 | + items: [ |
| 82 | + "Fusce pellentesque", |
| 83 | + "Aliquam porttitor", |
| 84 | + "Pellentesque", |
| 85 | + "Fusce pellentesque", |
| 86 | + "Aliquam porttitor", |
| 87 | + "Pellentesque", |
| 88 | + ], |
| 89 | + })} |
| 90 | + |
| 91 | + {renderLinks({ |
| 92 | + value: "products", |
| 93 | + items: ["Fusce pellentesque", "Aliquam porttitor", "Pellentesque"], |
| 94 | + })} |
| 95 | + </Presence> |
| 96 | + |
| 97 | + <Presence |
| 98 | + {...api.getContentProps({ value: "company" })} |
| 99 | + style={{ |
| 100 | + gridTemplateColumns: "1fr 1fr", |
| 101 | + width: 450, |
| 102 | + }} |
| 103 | + > |
| 104 | + {renderLinks({ |
| 105 | + value: "company", |
| 106 | + items: ["Fusce pellentesque", "Aliquam porttitor", "Pellentesque", "Aliquam porttitor"], |
| 107 | + })} |
| 108 | + |
| 109 | + {renderLinks({ |
| 110 | + value: "company", |
| 111 | + items: ["Fusce pellentesque", "Aliquam porttitor", "Pellentesque"], |
| 112 | + })} |
| 113 | + </Presence> |
| 114 | + |
| 115 | + <Presence |
| 116 | + {...api.getContentProps({ value: "developers" })} |
| 117 | + style={{ |
| 118 | + gridTemplateColumns: "1.6fr 1fr", |
| 119 | + width: 650, |
| 120 | + }} |
| 121 | + > |
| 122 | + {renderLinks({ |
| 123 | + value: "developers", |
| 124 | + items: ["Donec quis dui", "Vestibulum", "Fusce pellentesque", "Aliquam porttitor"], |
| 125 | + })} |
| 126 | + {renderLinks({ |
| 127 | + value: "developers", |
| 128 | + items: ["Fusce pellentesque", "Aliquam porttitor"], |
| 129 | + })} |
| 130 | + </Presence> |
| 131 | + </Presence> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + </Navbar> |
| 135 | + </main> |
| 136 | + |
| 137 | + <Toolbar controls={controls.ui} viz> |
| 138 | + <StateVisualizer state={service} context={["value", "previousValue"]} /> |
| 139 | + </Toolbar> |
| 140 | + </> |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +const Navbar = ({ children }: { children: React.ReactNode }) => { |
| 145 | + return ( |
| 146 | + <div |
| 147 | + style={{ |
| 148 | + position: "relative", |
| 149 | + display: "flex", |
| 150 | + boxSizing: "border-box", |
| 151 | + alignItems: "center", |
| 152 | + padding: "15px 20px", |
| 153 | + justifyContent: "space-between", |
| 154 | + width: "100%", |
| 155 | + backgroundColor: "white", |
| 156 | + boxShadow: "0 50px 100px -20px rgba(50,50,93,0.1),0 30px 60px -30px rgba(0,0,0,0.2)", |
| 157 | + }} |
| 158 | + > |
| 159 | + <button>Logo</button> |
| 160 | + {children} |
| 161 | + <button>Login</button> |
| 162 | + </div> |
| 163 | + ) |
| 164 | +} |
0 commit comments