@terrafirmagreg-team/ui 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. package/package.json +93 -3
  2. package/src/components/blocks/button/icon-button.tsx +48 -0
  3. package/src/components/blocks/canvas/chevron-edge.tsx +134 -0
  4. package/src/components/blocks/canvas/flow-canvas.tsx +177 -0
  5. package/src/components/blocks/canvas/flow-toolbar.tsx +32 -0
  6. package/src/components/blocks/canvas/flow-viewport.tsx +243 -0
  7. package/src/components/blocks/git/github-info.tsx +77 -0
  8. package/src/components/blocks/lang/content.tsx +22 -0
  9. package/src/components/blocks/lang/flag.tsx +24 -0
  10. package/src/components/blocks/lang/link.tsx +22 -0
  11. package/src/components/blocks/lang/redirect.tsx +47 -0
  12. package/src/components/blocks/lang/switcher.tsx +53 -0
  13. package/src/components/blocks/lang/untranslated-callout.tsx +28 -0
  14. package/src/components/blocks/link/link-preview.tsx +256 -0
  15. package/src/components/blocks/list/row.tsx +91 -0
  16. package/src/components/blocks/media/glb/model.tsx +243 -0
  17. package/src/components/blocks/media/glb/scene.tsx +372 -0
  18. package/src/components/blocks/media/glb/viewer.tsx +51 -0
  19. package/src/components/blocks/media/image.tsx +135 -0
  20. package/src/components/blocks/media/slideshow.tsx +177 -0
  21. package/src/components/blocks/nav/breadcrumb.tsx +50 -0
  22. package/src/components/blocks/nav/link.tsx +85 -0
  23. package/src/components/blocks/nav/site-menu.tsx +77 -0
  24. package/src/components/blocks/nav/site-title.tsx +60 -0
  25. package/src/components/blocks/nav/switcher.tsx +205 -0
  26. package/src/components/blocks/page/empty-state.tsx +31 -0
  27. package/src/components/blocks/page/heading.tsx +53 -0
  28. package/src/components/blocks/page/pager.tsx +112 -0
  29. package/src/components/blocks/readability/menu.tsx +431 -0
  30. package/src/components/blocks/readability/track.tsx +164 -0
  31. package/src/components/blocks/search/command.tsx +79 -0
  32. package/src/components/blocks/search/dialog.tsx +712 -0
  33. package/src/components/blocks/search/provider.tsx +117 -0
  34. package/src/components/blocks/sidebar.tsx +406 -0
  35. package/src/components/blocks/theme/toggle.tsx +71 -0
  36. package/src/components/blocks/tooltip/hint.tsx +27 -0
  37. package/src/components/blocks/viewer/controls.tsx +279 -0
  38. package/src/components/blocks/viewer/float-bar.tsx +50 -0
  39. package/src/components/layouts/layout.tsx +93 -0
  40. package/src/components/layouts/left-sidebar/content.tsx +62 -0
  41. package/src/components/layouts/left-sidebar/context.tsx +54 -0
  42. package/src/components/layouts/left-sidebar/expand.tsx +22 -0
  43. package/src/components/layouts/left-sidebar/footer.tsx +105 -0
  44. package/src/components/layouts/left-sidebar/index.ts +28 -0
  45. package/src/components/layouts/left-sidebar/items.tsx +102 -0
  46. package/src/components/layouts/left-sidebar/search.tsx +74 -0
  47. package/src/components/layouts/left-sidebar/sidebar.tsx +188 -0
  48. package/src/components/layouts/left-sidebar/tree.tsx +156 -0
  49. package/src/components/layouts/right-sidebar.tsx +95 -0
  50. package/src/components/layouts/tree-context.tsx +41 -0
  51. package/src/components/mdx/accordion.tsx +184 -0
  52. package/src/components/mdx/banner.tsx +154 -0
  53. package/src/components/mdx/callout.tsx +96 -0
  54. package/src/components/mdx/card.tsx +49 -0
  55. package/src/components/mdx/codeblock.tsx +249 -0
  56. package/src/components/mdx/collapsible.tsx +32 -0
  57. package/src/components/mdx/files.tsx +62 -0
  58. package/src/components/mdx/glow-header.tsx +40 -0
  59. package/src/components/mdx/gradient-text.tsx +27 -0
  60. package/src/components/mdx/image-zoom.tsx +314 -0
  61. package/src/components/mdx/mermaid.tsx +16 -0
  62. package/src/components/mdx/multiblock.tsx +18 -0
  63. package/src/components/mdx/preview.tsx +100 -0
  64. package/src/components/mdx/steps.tsx +9 -0
  65. package/src/components/mdx/table.tsx +13 -0
  66. package/src/components/mdx/tabs.tsx +353 -0
  67. package/src/components/mdx/type-table.tsx +170 -0
  68. package/src/components/ui/accordion.tsx +71 -0
  69. package/src/components/ui/alert-dialog.tsx +187 -0
  70. package/src/components/ui/alert.tsx +75 -0
  71. package/src/components/ui/aspect-ratio.tsx +22 -0
  72. package/src/components/ui/attachment.tsx +207 -0
  73. package/src/components/ui/avatar.tsx +108 -0
  74. package/src/components/ui/badge.tsx +51 -0
  75. package/src/components/ui/breadcrumb.tsx +124 -0
  76. package/src/components/ui/bubble.tsx +127 -0
  77. package/src/components/ui/button-group.tsx +87 -0
  78. package/src/components/ui/button.tsx +57 -0
  79. package/src/components/ui/calendar.tsx +221 -0
  80. package/src/components/ui/card.tsx +102 -0
  81. package/src/components/ui/carousel.tsx +242 -0
  82. package/src/components/ui/chart.tsx +371 -0
  83. package/src/components/ui/checkbox.tsx +28 -0
  84. package/src/components/ui/collapsible.tsx +21 -0
  85. package/src/components/ui/combobox.tsx +297 -0
  86. package/src/components/ui/command.tsx +198 -0
  87. package/src/components/ui/context-menu.tsx +270 -0
  88. package/src/components/ui/dialog.tsx +160 -0
  89. package/src/components/ui/direction.tsx +6 -0
  90. package/src/components/ui/drawer.tsx +227 -0
  91. package/src/components/ui/dropdown-menu.tsx +269 -0
  92. package/src/components/ui/empty.tsx +103 -0
  93. package/src/components/ui/field.tsx +238 -0
  94. package/src/components/ui/hover-card.tsx +50 -0
  95. package/src/components/ui/input-group.tsx +158 -0
  96. package/src/components/ui/input-otp.tsx +86 -0
  97. package/src/components/ui/input.tsx +19 -0
  98. package/src/components/ui/item.tsx +201 -0
  99. package/src/components/ui/kbd.tsx +26 -0
  100. package/src/components/ui/label.tsx +19 -0
  101. package/src/components/ui/marker.tsx +70 -0
  102. package/src/components/ui/menubar.tsx +280 -0
  103. package/src/components/ui/message-scroller.tsx +131 -0
  104. package/src/components/ui/message.tsx +91 -0
  105. package/src/components/ui/native-select.tsx +60 -0
  106. package/src/components/ui/navigation-menu.tsx +167 -0
  107. package/src/components/ui/pagination.tsx +130 -0
  108. package/src/components/ui/popover.tsx +91 -0
  109. package/src/components/ui/progress.tsx +82 -0
  110. package/src/components/ui/questionnaire.tsx +324 -0
  111. package/src/components/ui/radio-group.tsx +37 -0
  112. package/src/components/ui/resizable.tsx +49 -0
  113. package/src/components/ui/scroll-area.tsx +54 -0
  114. package/src/components/ui/select.tsx +200 -0
  115. package/src/components/ui/separator.tsx +24 -0
  116. package/src/components/ui/sheet.tsx +138 -0
  117. package/src/components/ui/sidebar.tsx +730 -0
  118. package/src/components/ui/skeleton.tsx +13 -0
  119. package/src/components/ui/slider.tsx +51 -0
  120. package/src/components/ui/spinner.tsx +10 -0
  121. package/src/components/ui/switch.tsx +31 -0
  122. package/src/components/ui/table.tsx +115 -0
  123. package/src/components/ui/tabs.tsx +81 -0
  124. package/src/components/ui/textarea.tsx +17 -0
  125. package/src/components/ui/toast.tsx +234 -0
  126. package/src/components/ui/toggle-group.tsx +89 -0
  127. package/src/components/ui/toggle.tsx +44 -0
  128. package/src/components/ui/tooltip.tsx +65 -0
  129. package/src/hooks/canvas/use-flow-hover.ts +18 -0
  130. package/src/hooks/canvas/use-flow-viewport-persist.ts +33 -0
  131. package/src/hooks/lang.ts +8 -0
  132. package/src/hooks/layout.ts +18 -0
  133. package/src/hooks/page/use-fit-pages.ts +91 -0
  134. package/src/hooks/page/use-location-search.ts +25 -0
  135. package/src/hooks/page/use-page-preview.ts +40 -0
  136. package/src/hooks/page/use-paged-list.ts +20 -0
  137. package/src/hooks/readability/provider.ts +86 -0
  138. package/src/hooks/search/dialog.ts +41 -0
  139. package/src/hooks/search/provider.ts +25 -0
  140. package/src/hooks/search/use-highlight-query.ts +51 -0
  141. package/src/hooks/sidebar.ts +63 -0
  142. package/src/hooks/use-async-value.ts +57 -0
  143. package/src/hooks/use-close-on-scroll.ts +23 -0
  144. package/src/hooks/use-copy-button.ts +46 -0
  145. package/src/hooks/use-cycle-index.ts +75 -0
  146. package/src/hooks/use-document-title.ts +9 -0
  147. package/src/hooks/use-element-size.ts +35 -0
  148. package/src/hooks/use-external-meta.ts +132 -0
  149. package/src/hooks/use-hot-key.ts +33 -0
  150. package/src/hooks/use-hover-follow.ts +54 -0
  151. package/src/hooks/use-in-view.ts +30 -0
  152. package/src/hooks/use-last-value.ts +15 -0
  153. package/src/hooks/use-marquee.ts +51 -0
  154. package/src/hooks/use-media-query.ts +14 -0
  155. package/src/hooks/use-mobile.ts +19 -0
  156. package/src/hooks/use-modifier-keys.ts +31 -0
  157. package/src/hooks/use-on-change.ts +35 -0
  158. package/src/hooks/use-pane-keys.ts +28 -0
  159. package/src/hooks/use-swipe.ts +26 -0
  160. package/src/hooks/use-tree.ts +102 -0
  161. package/src/hooks/viewer/use-in-float-bar.ts +6 -0
  162. package/src/hooks/viewer/use-viewer-prefs.ts +77 -0
  163. package/src/lib/canvas/edge.ts +173 -0
  164. package/src/lib/canvas/geometry.ts +53 -0
  165. package/src/lib/canvas/state.ts +47 -0
  166. package/src/lib/canvas/viewport.ts +37 -0
  167. package/src/lib/canvas/xyflow.ts +1 -0
  168. package/src/lib/config.ts +81 -0
  169. package/src/lib/mdx/callout.ts +20 -0
  170. package/src/lib/mdx/gradient.ts +15 -0
  171. package/src/lib/mdx/mermaid.ts +14 -0
  172. package/src/lib/media/crop-opaque.ts +76 -0
  173. package/src/lib/media/nearby-indexes.ts +6 -0
  174. package/src/lib/media/trim-transparent.ts +117 -0
  175. package/src/lib/readability/config.ts +10 -0
  176. package/src/lib/readability/spotlight.ts +186 -0
  177. package/src/lib/readability/store.ts +146 -0
  178. package/src/lib/tree.ts +92 -0
  179. package/src/lib/utils/async-cache.ts +32 -0
  180. package/src/lib/utils/cn.ts +1 -0
  181. package/src/lib/utils/format.ts +22 -0
  182. package/src/lib/utils/history.ts +9 -0
  183. package/src/lib/utils/merge-refs.ts +13 -0
  184. package/src/lib/utils/nav-active.ts +58 -0
  185. package/src/lib/utils/stored-value.ts +136 -0
  186. package/src/lib/utils/urls.ts +17 -0
  187. package/src/locales/en_us.json +119 -0
  188. package/src/locales/pl_pl.json +35 -0
  189. package/src/locales/ru_ru.json +119 -0
  190. package/src/locales/uk_ua.json +35 -0
  191. package/src/locales/zh_cn.json +17 -0
  192. package/src/locales/zh_tw.json +17 -0
  193. package/src/locales.ts +5 -0
  194. package/src/provider.tsx +141 -0
  195. package/src/types/canvas.ts +49 -0
  196. package/src/types/glb.ts +25 -0
  197. package/src/types/nav.ts +95 -0
  198. package/src/types/preview.ts +9 -0
  199. package/src/types/readability.ts +28 -0
  200. package/src/types/search.ts +56 -0
  201. package/src/types/tree.ts +18 -0
  202. package/src/types/viewer.ts +53 -0
  203. package/styles/content/base.css +194 -0
  204. package/styles/content/default-colors.css +51 -0
  205. package/styles/content/shiki.css +108 -0
  206. package/styles/content/typography.mjs +3941 -0
  207. package/styles/content.css +7 -0
  208. package/styles/image-zoom.css +83 -0
  209. package/styles/layout.css +28 -0
  210. package/styles/left-sidebar.css +140 -0
  211. package/styles/readability.css +50 -0
  212. package/styles/right-sidebar.css +6 -0
  213. package/styles/workspace.css +26 -0
  214. package/styles.css +413 -0
  215. package/README.md +0 -3
package/package.json CHANGED
@@ -1,6 +1,96 @@
1
1
  {
2
2
  "name": "@terrafirmagreg-team/ui",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": {
6
+ "./styles.css": "./styles.css",
7
+ "./styles/*": "./styles/*",
8
+ "./provider": "./src/provider.tsx",
9
+ "./components/ui/*": [
10
+ "./src/components/ui/*.tsx",
11
+ "./src/components/ui/*/index.tsx"
12
+ ],
13
+ "./components/blocks/*": [
14
+ "./src/components/blocks/*.tsx",
15
+ "./src/components/blocks/*/index.tsx"
16
+ ],
17
+ "./components/layouts/*": [
18
+ "./src/components/layouts/*.tsx",
19
+ "./src/components/layouts/*/index.tsx",
20
+ "./src/components/layouts/*/index.ts"
21
+ ],
22
+ "./hooks/*": [
23
+ "./src/hooks/*.ts",
24
+ "./src/hooks/*.tsx",
25
+ "./src/hooks/*/index.ts",
26
+ "./src/hooks/*/index.tsx"
27
+ ],
28
+ "./lib/*": [
29
+ "./src/lib/*.ts",
30
+ "./src/lib/*.tsx",
31
+ "./src/lib/*/index.ts",
32
+ "./src/lib/*/index.tsx"
33
+ ],
34
+ "./types/*": [
35
+ "./src/types/*.ts",
36
+ "./src/types/*/index.ts"
37
+ ],
38
+ "./components/mdx/*": [
39
+ "./src/components/mdx/*.tsx",
40
+ "./src/components/mdx/*/index.tsx"
41
+ ],
42
+ "./locales": "./src/locales.ts"
43
+ },
44
+ "dependencies": {
45
+ "@base-ui/react": "^1.9.0",
46
+ "@icons-pack/react-simple-icons": "13.15.1",
47
+ "@react-three/drei": "10.7.9",
48
+ "@react-three/fiber": "9.8.2",
49
+ "@shadcn/react": "^0.3.1",
50
+ "@terrafirmagreg-team/translate": "0.1.0",
51
+ "@xyflow/react": "12.12.0",
52
+ "beautiful-mermaid": "^1.1.3",
53
+ "class-variance-authority": "^0.7.1",
54
+ "cmdk": "^1.1.1",
55
+ "cn": "^0.5.0",
56
+ "embla-carousel-react": "^8.6.0",
57
+ "flag-icons": "^7.6.0",
58
+ "hast-util-to-jsx-runtime": "^2.3.6",
59
+ "input-otp": "^1.5.0",
60
+ "lucide-react": "^1.63.0",
61
+ "next-themes": "^0.4.6",
62
+ "react-day-picker": "^10.0.2",
63
+ "react-medium-image-zoom": "^5.4.9",
64
+ "react-resizable-panels": "^4.15.0",
65
+ "recharts": "3.10.1",
66
+ "scroll-into-view-if-needed": "^3.1.0",
67
+ "tailwindcss": "^4.3.3",
68
+ "three": "^0.186.1",
69
+ "three-stdlib": "2.36.1"
70
+ },
71
+ "peerDependencies": {
72
+ "next": ">=15",
73
+ "react": ">=19",
74
+ "react-dom": ">=19"
75
+ },
76
+ "devDependencies": {
77
+ "@types/hast": "^3.0.5",
78
+ "@types/node": "^26.6.5",
79
+ "@types/react": "^19.3.0",
80
+ "@types/react-dom": "^19.3.0",
81
+ "@types/three": "^0.186.0"
82
+ },
83
+ "repository": {
84
+ "type": "git",
85
+ "url": "https://github.com/TerraFirmaGreg-Team/Web-Foundation.git",
86
+ "directory": "packages/ui"
87
+ },
88
+ "publishConfig": {
89
+ "access": "public"
90
+ },
91
+ "files": [
92
+ "src",
93
+ "styles",
94
+ "styles.css"
95
+ ]
6
96
  }
@@ -0,0 +1,48 @@
1
+ 'use client';
2
+ import type { ComponentProps } from 'react';
3
+ import { Button } from '@terrafirmagreg-team/ui/components/ui/button';
4
+ import {
5
+ Tooltip,
6
+ TooltipContent,
7
+ TooltipTrigger,
8
+ } from '@terrafirmagreg-team/ui/components/ui/tooltip';
9
+ import { useInFloatBar } from '@terrafirmagreg-team/ui/hooks/viewer/use-in-float-bar';
10
+ import { cn } from '@terrafirmagreg-team/ui/lib/utils/cn';
11
+
12
+ export function IconButton({
13
+ label,
14
+ tooltip = true,
15
+ overlay,
16
+ variant = 'outline',
17
+ size = 'icon-sm',
18
+ className,
19
+ ...props
20
+ }: ComponentProps<typeof Button> & {
21
+ label: string;
22
+ tooltip?: boolean;
23
+ overlay?: boolean;
24
+ }) {
25
+ const inFloatBar = useInFloatBar();
26
+ const button = (
27
+ <Button
28
+ variant={overlay && variant === 'outline' ? 'ghost' : variant}
29
+ size={size}
30
+ aria-label={label}
31
+ className={cn(
32
+ overlay &&
33
+ (inFloatBar
34
+ ? 'rounded-none'
35
+ : 'bg-float/92 backdrop-blur-md hover:bg-float'),
36
+ className,
37
+ )}
38
+ {...props}
39
+ />
40
+ );
41
+ if (!tooltip) return button;
42
+ return (
43
+ <Tooltip>
44
+ <TooltipTrigger render={button} />
45
+ <TooltipContent>{label}</TooltipContent>
46
+ </Tooltip>
47
+ );
48
+ }
@@ -0,0 +1,134 @@
1
+ 'use client';
2
+
3
+ import { memo, useMemo, type FC } from 'react';
4
+ import {
5
+ useInternalNode,
6
+ useStore,
7
+ type EdgeProps,
8
+ type InternalNode,
9
+ } from '@terrafirmagreg-team/ui/lib/canvas/xyflow';
10
+ import { cn } from '@terrafirmagreg-team/ui/lib/utils/cn';
11
+ import {
12
+ chevronHighlightClass,
13
+ chevronTicksAlongPath,
14
+ } from '@terrafirmagreg-team/ui/lib/canvas/edge';
15
+ import type {
16
+ ChevronEdgeHighlight,
17
+ FlowPoint,
18
+ } from '@terrafirmagreg-team/ui/types/canvas';
19
+
20
+ export type ChevronEdgePathContext = EdgeProps & {
21
+ sourceNode: InternalNode;
22
+ targetNode: InternalNode;
23
+ };
24
+
25
+ type ChevronEdgeResolvers = {
26
+ resolveCenter?: (node: InternalNode) => FlowPoint;
27
+ resolveBorder?: (node: InternalNode, toward: FlowPoint) => FlowPoint;
28
+ resolvePath?: (ctx: ChevronEdgePathContext) => FlowPoint[];
29
+ className?: string;
30
+ };
31
+
32
+ const CHEVRON_TICK_MIN_ZOOM = 0.25;
33
+
34
+ export function createChevronEdge({
35
+ resolveCenter,
36
+ resolveBorder,
37
+ resolvePath,
38
+ className,
39
+ }: ChevronEdgeResolvers): FC<EdgeProps> {
40
+ return memo(function ChevronEdge(props: EdgeProps) {
41
+ const { source, target, data } = props;
42
+ const sourceNode = useInternalNode(source);
43
+ const targetNode = useInternalNode(target);
44
+ const farZoom = useStore(
45
+ (state) => state.transform[2] < CHEVRON_TICK_MIN_ZOOM,
46
+ );
47
+ const sAbs = sourceNode?.internals.positionAbsolute;
48
+ const tAbs = targetNode?.internals.positionAbsolute;
49
+ const sSize = sourceNode?.measured;
50
+ const tSize = targetNode?.measured;
51
+ const {
52
+ sourceX,
53
+ sourceY,
54
+ sourcePosition,
55
+ targetX,
56
+ targetY,
57
+ targetPosition,
58
+ } = props;
59
+
60
+ const points = useMemo(() => {
61
+ if (!sAbs || !tAbs || !sourceNode || !targetNode) return null;
62
+ return resolvePath
63
+ ? resolvePath({ ...props, sourceNode, targetNode })
64
+ : resolveCenter && resolveBorder
65
+ ? [
66
+ resolveBorder(sourceNode, resolveCenter(targetNode)),
67
+ resolveBorder(targetNode, resolveCenter(sourceNode)),
68
+ ]
69
+ : [
70
+ { x: sourceX, y: sourceY },
71
+ { x: targetX, y: targetY },
72
+ ];
73
+ // eslint-disable-next-line react-hooks/exhaustive-deps
74
+ }, [
75
+ data,
76
+ sAbs?.x,
77
+ sAbs?.y,
78
+ sSize?.width,
79
+ sSize?.height,
80
+ tAbs?.x,
81
+ tAbs?.y,
82
+ tSize?.width,
83
+ tSize?.height,
84
+ sourceX,
85
+ sourceY,
86
+ sourcePosition,
87
+ targetX,
88
+ targetY,
89
+ targetPosition,
90
+ ]);
91
+ const runs = useMemo(
92
+ () => (points && !farZoom ? chevronTicksAlongPath(points) : []),
93
+ [points, farZoom],
94
+ );
95
+ const highlight = data?.highlight as ChevronEdgeHighlight;
96
+ if (data?.hidden === true) return null;
97
+ if (!points || points.length < 2) return null;
98
+ const base = `M${points[0]!.x} ${points[0]!.y}${points
99
+ .slice(1)
100
+ .map((p) => `L${p.x} ${p.y}`)
101
+ .join('')}`;
102
+ return (
103
+ <g
104
+ className={cn(
105
+ 'chevron-edge',
106
+ className,
107
+ chevronHighlightClass(highlight),
108
+ )}
109
+ >
110
+ <path
111
+ className="chevron-edge__stroke chevron-edge__base"
112
+ d={base}
113
+ aria-hidden="true"
114
+ />
115
+ {runs.map((run, i) => (
116
+ <g
117
+ key={i}
118
+ className="chevron-edge__ticks"
119
+ style={{
120
+ ['--chevron-edge-shift-x' as string]: `${run.shiftX}px`,
121
+ ['--chevron-edge-shift-y' as string]: `${run.shiftY}px`,
122
+ }}
123
+ >
124
+ <path
125
+ className="chevron-edge__stroke chevron-edge__stroke--main"
126
+ d={run.d}
127
+ aria-hidden="true"
128
+ />
129
+ </g>
130
+ ))}
131
+ </g>
132
+ );
133
+ });
134
+ }
@@ -0,0 +1,177 @@
1
+ 'use client';
2
+
3
+ import {
4
+ useCallback,
5
+ useMemo,
6
+ useRef,
7
+ type ComponentProps,
8
+ type ReactNode,
9
+ } from 'react';
10
+ import {
11
+ Background,
12
+ ReactFlow,
13
+ useReactFlow,
14
+ type Edge,
15
+ type Node,
16
+ type ReactFlowProps,
17
+ } from '@terrafirmagreg-team/ui/lib/canvas/xyflow';
18
+ import { useElementSize } from '@terrafirmagreg-team/ui/hooks/use-element-size';
19
+ import { useFlowViewportPersist } from '@terrafirmagreg-team/ui/hooks/canvas/use-flow-viewport-persist';
20
+ import { cn } from '@terrafirmagreg-team/ui/lib/utils/cn';
21
+ import {
22
+ flowContentBox,
23
+ zoomToFitBox,
24
+ } from '@terrafirmagreg-team/ui/lib/canvas/geometry';
25
+ import {
26
+ FLOW_MAX_ZOOM,
27
+ FLOW_MIN_ZOOM,
28
+ } from '@terrafirmagreg-team/ui/lib/canvas/viewport';
29
+ import { FlowViewport } from '@terrafirmagreg-team/ui/components/blocks/canvas/flow-viewport';
30
+ import type {
31
+ FlowCanvasViewport,
32
+ FlowRect,
33
+ } from '@terrafirmagreg-team/ui/types/canvas';
34
+
35
+ export type FlowCanvasFit = { onFit: () => void };
36
+
37
+ export type FlowCanvasProps<
38
+ N extends Node = Node,
39
+ E extends Edge = Edge,
40
+ > = Omit<ReactFlowProps<N, E>, 'nodes' | 'children'> & {
41
+ nodes: N[];
42
+ viewportConfig: FlowCanvasViewport;
43
+
44
+ extraRects?: FlowRect[];
45
+
46
+ fitNodes?: (node: N) => boolean;
47
+ backgroundGap?: number;
48
+ className?: string;
49
+ shellProps?: ComponentProps<'div'> &
50
+ Record<`data-${string}`, unknown>;
51
+
52
+ children?: ReactNode | ((fit: FlowCanvasFit) => ReactNode);
53
+ };
54
+
55
+ export function FlowCanvas<N extends Node = Node, E extends Edge = Edge>({
56
+ nodes,
57
+ viewportConfig,
58
+ extraRects,
59
+ fitNodes,
60
+ backgroundGap = 16,
61
+ className,
62
+ shellProps,
63
+ children,
64
+ onMoveEnd,
65
+ ...flowProps
66
+ }: FlowCanvasProps<N, E>) {
67
+ const canvasRef = useRef<HTMLDivElement>(null);
68
+ const viewSize = useElementSize(canvasRef);
69
+ const { setCenter } = useReactFlow();
70
+ const { markReady, persist } = useFlowViewportPersist(
71
+ viewportConfig.storageKey,
72
+ viewportConfig.resetKey,
73
+ );
74
+ const contentBox = useMemo(
75
+ () => flowContentBox(nodes, extraRects),
76
+ [nodes, extraRects],
77
+ );
78
+ const scoped = useMemo(
79
+ () => (fitNodes ? nodes.filter(fitNodes) : nodes),
80
+ [nodes, fitNodes],
81
+ );
82
+ const fitBox = useMemo(() => {
83
+ if (!fitNodes || !scoped.length) return contentBox;
84
+ return flowContentBox(scoped, extraRects);
85
+ }, [scoped, extraRects, fitNodes, contentBox]);
86
+ const { focusZoom = 1, persist: _persist, ...viewportRest } = viewportConfig;
87
+ const onFit = useCallback(() => {
88
+ if (!fitBox || viewSize.w < 1 || viewSize.h < 1) return;
89
+ const fitZoom = zoomToFitBox(
90
+ fitBox,
91
+ viewSize.w,
92
+ viewSize.h,
93
+ flowProps.minZoom,
94
+ flowProps.maxZoom,
95
+ );
96
+
97
+ const pool = scoped.length ? scoped : nodes;
98
+ let anchor: { x: number; y: number } = {
99
+ x: fitBox.midX,
100
+ y: fitBox.midY,
101
+ };
102
+ let best = Infinity;
103
+ for (const node of pool) {
104
+ const dx = node.position.x - fitBox.midX;
105
+ const dy = node.position.y - fitBox.midY;
106
+ const dist = dx * dx + dy * dy;
107
+ if (dist < best) {
108
+ best = dist;
109
+ anchor = node.position;
110
+ }
111
+ }
112
+
113
+ void setCenter(anchor.x, anchor.y, {
114
+ zoom: Math.max(fitZoom, focusZoom * 0.3),
115
+ duration: 200,
116
+ });
117
+ }, [
118
+ fitBox,
119
+ scoped,
120
+ nodes,
121
+ setCenter,
122
+ viewSize,
123
+ flowProps.minZoom,
124
+ flowProps.maxZoom,
125
+ focusZoom,
126
+ ]);
127
+ const handleMoveEnd = useCallback<
128
+ NonNullable<ReactFlowProps<N, E>['onMoveEnd']>
129
+ >(
130
+ (event, vp) => {
131
+ if (viewportConfig.persist) persist(vp);
132
+ onMoveEnd?.(event, vp);
133
+ },
134
+ [viewportConfig.persist, persist, onMoveEnd],
135
+ );
136
+
137
+ return (
138
+ <div
139
+ ref={canvasRef}
140
+ suppressHydrationWarning
141
+ {...shellProps}
142
+ className={cn(
143
+ 'flex-1 min-h-0 h-full not-prose relative w-full overflow-hidden',
144
+ className,
145
+ shellProps?.className,
146
+ )}
147
+ >
148
+ {viewSize.w > 0 && viewSize.h > 0 ? (
149
+ <ReactFlow<N, E>
150
+ nodes={nodes}
151
+ nodeOrigin={[0.5, 0.5]}
152
+ nodesConnectable={false}
153
+ elementsSelectable
154
+ panOnDrag
155
+ zoomOnPinch
156
+ minZoom={FLOW_MIN_ZOOM}
157
+ maxZoom={FLOW_MAX_ZOOM}
158
+ proOptions={{ hideAttribution: true }}
159
+ className="h-full bg-card"
160
+ onMoveEnd={handleMoveEnd}
161
+ {...flowProps}
162
+ >
163
+ <Background gap={backgroundGap} size={1} />
164
+ {typeof children === 'function' ? children({ onFit }) : children}
165
+ <FlowViewport
166
+ {...viewportRest}
167
+ nodes={nodes}
168
+ contentBox={contentBox}
169
+ viewSize={viewSize}
170
+ focusZoom={focusZoom}
171
+ onReady={markReady}
172
+ />
173
+ </ReactFlow>
174
+ ) : null}
175
+ </div>
176
+ );
177
+ }
@@ -0,0 +1,32 @@
1
+ 'use client';
2
+
3
+ import { useReactFlow } from '@terrafirmagreg-team/ui/lib/canvas/xyflow';
4
+ import { Search } from 'lucide-react';
5
+ import {
6
+ ViewerToolbar,
7
+ ViewerControls,
8
+ } from '@terrafirmagreg-team/ui/components/blocks/viewer/controls';
9
+ import { IconButton } from '@terrafirmagreg-team/ui/components/blocks/button/icon-button';
10
+ import type { FlowToolbarProps } from '@terrafirmagreg-team/ui/types/canvas';
11
+
12
+ export function FlowToolbar({ onFit, search }: FlowToolbarProps) {
13
+ const { zoomIn, zoomOut } = useReactFlow();
14
+ return (
15
+ <ViewerToolbar>
16
+ <ViewerControls
17
+ start={
18
+ search ? (
19
+ <IconButton overlay label={search.label} onClick={search.onOpen}>
20
+ <Search className="size-3.5" />
21
+ </IconButton>
22
+ ) : null
23
+ }
24
+ zoom={{
25
+ onZoomIn: () => void zoomIn({ duration: 150 }),
26
+ onZoomOut: () => void zoomOut({ duration: 150 }),
27
+ onFit,
28
+ }}
29
+ />
30
+ </ViewerToolbar>
31
+ );
32
+ }