@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
@@ -0,0 +1,53 @@
1
+ 'use client';
2
+ import { CopyCheckIcon, LinkIcon } from 'lucide-react';
3
+ import type { ComponentProps } from 'react';
4
+ import { cn } from '@terrafirmagreg-team/ui/lib/utils/cn';
5
+ import { buttonVariants } from '@terrafirmagreg-team/ui/components/ui/button';
6
+ import { useCopyButton } from '@terrafirmagreg-team/ui/hooks/use-copy-button';
7
+ import { useTranslations } from '@terrafirmagreg-team/translate/hooks/use-translations';
8
+
9
+ type HeadingTag = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
10
+
11
+ export function Heading({
12
+ as,
13
+ ...props
14
+ }: ComponentProps<'h1'> & { as?: HeadingTag }) {
15
+ const As = as ?? 'h1';
16
+ const t = useTranslations();
17
+ const { checked: isChecked, onClick: onCopy } = useCopyButton(() => {
18
+ if (!props.id) return;
19
+ const url = new URL(window.location.href);
20
+ url.hash = props.id;
21
+ return navigator.clipboard.writeText(url.href);
22
+ });
23
+
24
+ if (!props.id) return <As {...props} />;
25
+ return (
26
+ <As
27
+ {...props}
28
+ className={cn(
29
+ 'group/heading flex scroll-m-28 flex-row items-center gap-1',
30
+ props.className,
31
+ )}
32
+ >
33
+ <a data-card href={`#${props.id}`}>
34
+ {props.children}
35
+ </a>
36
+ <button
37
+ aria-live="polite"
38
+ className={cn(
39
+ buttonVariants({ variant: 'ghost', size: 'icon-xs' }),
40
+ 'not-prose shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover/heading:opacity-100',
41
+ )}
42
+ onClick={onCopy}
43
+ >
44
+ {isChecked ? <CopyCheckIcon /> : <LinkIcon />}
45
+ <span className="sr-only">
46
+ {isChecked
47
+ ? t('ui.copied_anchor', )
48
+ : t('ui.copy_anchor', )}
49
+ </span>
50
+ </button>
51
+ </As>
52
+ );
53
+ }
@@ -0,0 +1,112 @@
1
+ 'use client';
2
+
3
+ import { ChevronLeft, ChevronRight } from 'lucide-react';
4
+ import type { MouseEvent, ReactNode } from 'react';
5
+ import { IconButton } from '@terrafirmagreg-team/ui/components/blocks/button/icon-button';
6
+ import {
7
+ Pagination,
8
+ PaginationContent,
9
+ PaginationItem,
10
+ } from '@terrafirmagreg-team/ui/components/ui/pagination';
11
+ import { Progress } from '@terrafirmagreg-team/ui/components/ui/progress';
12
+ import { useTranslations } from '@terrafirmagreg-team/translate/hooks/use-translations';
13
+ import { cn } from '@terrafirmagreg-team/ui/lib/utils/cn';
14
+
15
+ export function PagerArrow({
16
+ dir,
17
+ disabled,
18
+ onClick,
19
+ label,
20
+ variant = 'secondary',
21
+ size = 'icon-lg',
22
+ overlay,
23
+ className,
24
+ }: {
25
+ dir: -1 | 1;
26
+ disabled?: boolean;
27
+ onClick: (event: MouseEvent<HTMLButtonElement>) => void;
28
+ label?: string;
29
+ variant?: 'outline' | 'secondary' | 'ghost';
30
+ size?: 'icon-xs' | 'icon-sm' | 'icon-lg';
31
+ overlay?: boolean;
32
+ className?: string;
33
+ }) {
34
+ const t = useTranslations();
35
+ return (
36
+ <IconButton
37
+ label={label ?? t(dir < 0 ? 'ui.prev_page' : 'ui.next_page')}
38
+ tooltip={false}
39
+ variant={variant}
40
+ size={size}
41
+ overlay={overlay}
42
+ disabled={disabled}
43
+ onClick={onClick}
44
+ className={cn(
45
+ 'shrink-0',
46
+ variant === 'secondary' &&
47
+ 'border-foreground/10 hover:bg-background dark:hover:bg-accent',
48
+ className,
49
+ )}
50
+ >
51
+ {dir < 0 ? (
52
+ <ChevronLeft className="size-[58%]" />
53
+ ) : (
54
+ <ChevronRight className="size-[58%]" />
55
+ )}
56
+ </IconButton>
57
+ );
58
+ }
59
+
60
+ export function Pager({
61
+ page,
62
+ pages,
63
+ to,
64
+ total,
65
+ onPage,
66
+ label,
67
+ leading,
68
+ className,
69
+ }: {
70
+ page: number;
71
+ pages: number;
72
+ to: number;
73
+ total: number;
74
+ onPage: (page: number) => void;
75
+ label: string;
76
+ leading?: ReactNode;
77
+ className?: string;
78
+ }) {
79
+ return (
80
+ <Pagination className={cn('mx-0 w-full', className)}>
81
+ <PaginationContent className="w-full justify-between gap-2">
82
+ <PaginationItem className="flex items-center gap-1">
83
+ <PagerArrow
84
+ dir={-1}
85
+ disabled={page <= 0}
86
+ onClick={(event) => onPage(event.shiftKey ? 0 : page - 1)}
87
+ />
88
+ {leading}
89
+ </PaginationItem>
90
+ <PaginationItem className="flex min-w-0 items-center gap-2 text-xs text-muted-foreground">
91
+ <Progress
92
+ value={to}
93
+ max={Math.max(total, 1)}
94
+ className="w-14 shrink-0 gap-0 sm:w-20"
95
+ />
96
+ <span className="shrink-0 tabular-nums">
97
+ {label
98
+ .replaceAll('{page}', String(page + 1))
99
+ .replaceAll('{pages}', String(pages))}
100
+ </span>
101
+ </PaginationItem>
102
+ <PaginationItem>
103
+ <PagerArrow
104
+ dir={1}
105
+ disabled={page + 1 >= pages}
106
+ onClick={(event) => onPage(event.shiftKey ? pages - 1 : page + 1)}
107
+ />
108
+ </PaginationItem>
109
+ </PaginationContent>
110
+ </Pagination>
111
+ );
112
+ }
@@ -0,0 +1,431 @@
1
+ 'use client';
2
+ import { usePathname } from 'next/navigation';
3
+ import { useState, type ComponentProps, type ReactNode } from 'react';
4
+ import {
5
+ AlignStartVertical,
6
+ CircleHelp,
7
+ Columns2,
8
+ Flashlight,
9
+ FoldHorizontal,
10
+ Layers,
11
+ Maximize2,
12
+ Minimize2,
13
+ Palette,
14
+ PanelLeft,
15
+ Proportions,
16
+ Settings,
17
+ UnfoldHorizontal,
18
+ } from 'lucide-react';
19
+ import { cn } from '@terrafirmagreg-team/ui/lib/utils/cn';
20
+ import { useTranslations } from '@terrafirmagreg-team/translate/hooks/use-translations';
21
+ import { useReadability } from '@terrafirmagreg-team/ui/hooks/readability/provider';
22
+ import { readabilityConfig } from '@terrafirmagreg-team/ui/lib/readability/config';
23
+ import {
24
+ Popover,
25
+ PopoverContent,
26
+ PopoverTrigger,
27
+ } from '@terrafirmagreg-team/ui/components/ui/popover';
28
+ import { IconButton } from '@terrafirmagreg-team/ui/components/blocks/button/icon-button';
29
+ import { Hint } from '@terrafirmagreg-team/ui/components/blocks/tooltip/hint';
30
+ import {
31
+ Field,
32
+ FieldContent,
33
+ FieldDescription,
34
+ FieldLabel,
35
+ } from '@terrafirmagreg-team/ui/components/ui/field';
36
+ import { Slider } from '@terrafirmagreg-team/ui/components/ui/slider';
37
+ import {
38
+ ToggleGroup,
39
+ ToggleGroupItem,
40
+ } from '@terrafirmagreg-team/ui/components/ui/toggle-group';
41
+ import {
42
+ readabilityLayouts,
43
+ spotlightStyles,
44
+ type ReadabilityLayout,
45
+ type SpotlightStyle,
46
+ } from '@terrafirmagreg-team/ui/types/readability';
47
+
48
+ const layoutIcons: Record<ReadabilityLayout, ReactNode> = {
49
+ full: <Maximize2 />,
50
+ sidebar: <PanelLeft />,
51
+ both: <Columns2 />,
52
+ original: <Minimize2 />,
53
+ };
54
+
55
+ const styleIcons: Record<SpotlightStyle, ReactNode> = {
56
+ under: <Layers />,
57
+ aside: <AlignStartVertical />,
58
+ };
59
+
60
+ const switches = ['on', 'off'] as const;
61
+
62
+ function ReadabilityHelp({
63
+ title,
64
+ description,
65
+ items,
66
+ }: {
67
+ title: string;
68
+ description?: string;
69
+ items?: { icon?: ReactNode; label: string; help: string }[];
70
+ }) {
71
+ return (
72
+ <div className="space-y-2">
73
+ <div className="space-y-1">
74
+ <p className="font-medium">{title}</p>
75
+ {description && <FieldDescription className="text-xs">{description}</FieldDescription>}
76
+ </div>
77
+ {items?.map((item) => (
78
+ <div key={item.label} className="space-y-1 rounded-lg bg-muted/60 p-2.5">
79
+ <p className="flex items-center gap-1.5 text-xs font-medium [&_svg:not([class*='size-'])]:size-3.5 [&_svg]:shrink-0">
80
+ {item.icon}
81
+ {item.label}
82
+ </p>
83
+ <FieldDescription className="text-xs">{item.help}</FieldDescription>
84
+ </div>
85
+ ))}
86
+ </div>
87
+ );
88
+ }
89
+
90
+ function ReadabilityField({
91
+ label,
92
+ icon,
93
+ help,
94
+ children,
95
+ }: {
96
+ label: ReactNode;
97
+ icon?: ReactNode;
98
+ help?: ReactNode;
99
+ children: ReactNode;
100
+ }) {
101
+ const [helpOpen, setHelpOpen] = useState(false);
102
+ return (
103
+ <Field className="gap-1.5">
104
+ <div className="flex items-center gap-1 [&_svg:not([class*='size-'])]:size-3.5 [&_svg]:shrink-0">
105
+ {icon}
106
+ <FieldLabel className="flex-1 text-xs text-muted-foreground">{label}</FieldLabel>
107
+ {help && (
108
+ <Popover onOpenChange={setHelpOpen}>
109
+ <PopoverTrigger
110
+ render={
111
+ <IconButton
112
+ variant="ghost"
113
+ size="icon-xs"
114
+ tooltip={false}
115
+ label={typeof label === 'string' ? label : 'Help'}
116
+ >
117
+ <CircleHelp />
118
+ </IconButton>
119
+ }
120
+ />
121
+ <PopoverContent className="w-56">{help}</PopoverContent>
122
+ </Popover>
123
+ )}
124
+ </div>
125
+ <FieldContent
126
+ className={cn(
127
+ 'rounded-lg outline-2 outline-offset-4 outline-dashed outline-transparent transition-[outline-color] duration-200',
128
+ helpOpen && 'outline-primary',
129
+ )}
130
+ >
131
+ {children}
132
+ </FieldContent>
133
+ </Field>
134
+ );
135
+ }
136
+
137
+ function ReadabilitySlider({
138
+ label,
139
+ icon,
140
+ help,
141
+ value,
142
+ onChange,
143
+ min = 0,
144
+ max = 100,
145
+ step = 1,
146
+ format = String,
147
+ disabled,
148
+ live,
149
+ }: {
150
+ label: ReactNode;
151
+ icon?: ReactNode;
152
+ help?: ReactNode;
153
+ value: number;
154
+ onChange: (value: number) => void;
155
+ min?: number;
156
+ max?: number;
157
+ step?: number;
158
+ format?: (value: number) => string;
159
+ disabled?: boolean;
160
+ live?: boolean;
161
+ }) {
162
+ const [draft, setDraft] = useState<{ key: number; value: number }>({ key: value, value });
163
+ const shown = draft.key === value ? draft.value : value;
164
+ return (
165
+ <ReadabilityField label={label} icon={icon} help={help}>
166
+ <div className="flex items-center gap-3 rounded-lg bg-muted/70 px-3 py-2.5">
167
+ <Slider
168
+ min={min}
169
+ max={max}
170
+ step={step}
171
+ value={shown}
172
+ disabled={disabled}
173
+ className="flex-1 [&_[data-slot=slider-track]]:bg-muted-foreground/25"
174
+ onValueChange={(next) => {
175
+ if (live) onChange(next as number);
176
+ else setDraft({ key: value, value: next as number });
177
+ }}
178
+ onValueCommitted={() => {
179
+ if (!live && shown !== value) onChange(shown);
180
+ }}
181
+ />
182
+ <span className="w-9 shrink-0 text-end text-xs tabular-nums text-muted-foreground">
183
+ {format(shown)}
184
+ </span>
185
+ </div>
186
+ </ReadabilityField>
187
+ );
188
+ }
189
+
190
+ function Segment<T extends string>({
191
+ options,
192
+ value,
193
+ onChange,
194
+ disabled,
195
+ itemContent,
196
+ itemLabel,
197
+ itemHelp,
198
+ itemClassName,
199
+ }: {
200
+ options: readonly T[];
201
+ value: T;
202
+ onChange: (value: T) => void;
203
+ disabled?: boolean;
204
+ itemContent: (id: T) => ReactNode;
205
+ itemLabel: (id: T) => string;
206
+ itemHelp: (id: T) => string;
207
+ itemClassName?: string;
208
+ }) {
209
+ return (
210
+ <ToggleGroup
211
+ value={[value]}
212
+ onValueChange={(values) => {
213
+ const next = values.at(-1) as T | undefined;
214
+ if (next && next !== value) onChange(next);
215
+ }}
216
+ disabled={disabled}
217
+ size="sm"
218
+ spacing={1}
219
+ className="w-full rounded-lg bg-muted/70 p-1"
220
+ >
221
+ {options.map((id) => (
222
+ <Hint key={id} label={itemHelp(id)}>
223
+ <ToggleGroupItem
224
+ value={id}
225
+ aria-label={itemLabel(id)}
226
+ className={cn(
227
+ 'flex-1 rounded-md text-muted-foreground hover:text-foreground aria-pressed:bg-background aria-pressed:text-foreground aria-pressed:shadow-sm',
228
+ itemClassName,
229
+ )}
230
+ >
231
+ {itemContent(id)}
232
+ </ToggleGroupItem>
233
+ </Hint>
234
+ ))}
235
+ </ToggleGroup>
236
+ );
237
+ }
238
+
239
+ function WidthField({
240
+ field,
241
+ icon,
242
+ value,
243
+ onChange,
244
+ }: {
245
+ field: 'page_width' | 'content_width';
246
+ icon: ReactNode;
247
+ value: number;
248
+ onChange: (value: number) => void;
249
+ }) {
250
+ const t = useTranslations();
251
+ return (
252
+ <ReadabilitySlider
253
+ live
254
+ label={t(`ui.readability.${field}`)}
255
+ icon={icon}
256
+ help={
257
+ <ReadabilityHelp
258
+ title={t(`ui.readability.${field}`)}
259
+ description={t(`ui.readability.${field}.help`)}
260
+ items={[
261
+ {
262
+ icon,
263
+ label: t(`ui.readability.${field}.slider`),
264
+ help: t(`ui.readability.${field}.slider.help`),
265
+ },
266
+ ]}
267
+ />
268
+ }
269
+ value={value}
270
+ onChange={onChange}
271
+ min={60}
272
+ max={100}
273
+ step={1}
274
+ format={(v) => `${v}%`}
275
+ />
276
+ );
277
+ }
278
+
279
+ export function ReadabilityMenu({
280
+ anchor,
281
+ }: {
282
+ anchor?: ComponentProps<typeof PopoverContent>['anchor'];
283
+ }) {
284
+ const t = useTranslations();
285
+ const {
286
+ layout,
287
+ setLayout,
288
+ pageWidth,
289
+ setPageWidth,
290
+ contentWidth,
291
+ setContentWidth,
292
+ spotlight,
293
+ setSpotlight,
294
+ spotlightStyle,
295
+ setSpotlightStyle,
296
+ coarsePointer,
297
+ smallScreen,
298
+ } = useReadability();
299
+
300
+ const layoutDisabled = smallScreen;
301
+ const pathname = usePathname();
302
+ const spotlightDisabled =
303
+ coarsePointer || (readabilityConfig().isSpotlightDisabled?.(pathname) ?? false);
304
+
305
+ return (
306
+ <Popover>
307
+ <PopoverTrigger
308
+ render={
309
+ <IconButton
310
+ variant="ghost"
311
+ size="icon-sm"
312
+ tooltip={false}
313
+ label={t('ui.readability.menu')}
314
+ className="text-muted-foreground! data-[popup-open]:bg-accent! data-[popup-open]:text-accent-foreground!"
315
+ >
316
+ <Settings />
317
+ </IconButton>
318
+ }
319
+ />
320
+ <PopoverContent
321
+ anchor={anchor}
322
+ side="top"
323
+ align="center"
324
+ sideOffset={8}
325
+ className="w-64 space-y-3 p-3"
326
+ >
327
+ <ReadabilityField
328
+ label={t('ui.readability.layout')}
329
+ icon={<Proportions />}
330
+ help={
331
+ <ReadabilityHelp
332
+ title={t('ui.readability.layout')}
333
+ description={
334
+ layoutDisabled
335
+ ? t('ui.readability.layout.unavailable')
336
+ : t('ui.readability.layout.help')
337
+ }
338
+ items={readabilityLayouts.map((id) => ({
339
+ icon: layoutIcons[id],
340
+ label: t(`ui.readability.layout.${id}`),
341
+ help: t(`ui.readability.layout.${id}.help`),
342
+ }))}
343
+ />
344
+ }
345
+ >
346
+ <Segment
347
+ options={readabilityLayouts}
348
+ value={layout}
349
+ onChange={setLayout}
350
+ disabled={layoutDisabled}
351
+ itemContent={(id) => layoutIcons[id]}
352
+ itemLabel={(id) => t(`ui.readability.layout.${id}`)}
353
+ itemHelp={(id) => t(`ui.readability.layout.${id}.help`)}
354
+ />
355
+ </ReadabilityField>
356
+ {(layout === 'sidebar' || layout === 'both') && !smallScreen && (
357
+ <WidthField
358
+ field="page_width"
359
+ icon={<UnfoldHorizontal />}
360
+ value={pageWidth}
361
+ onChange={setPageWidth}
362
+ />
363
+ )}
364
+ {layout === 'both' && !smallScreen && (
365
+ <WidthField
366
+ field="content_width"
367
+ icon={<FoldHorizontal />}
368
+ value={contentWidth}
369
+ onChange={setContentWidth}
370
+ />
371
+ )}
372
+ <ReadabilityField
373
+ label={t('ui.readability.spotlight')}
374
+ icon={<Flashlight />}
375
+ help={
376
+ <ReadabilityHelp
377
+ title={t('ui.readability.spotlight')}
378
+ description={
379
+ spotlightDisabled
380
+ ? t('ui.readability.spotlight.unavailable')
381
+ : t('ui.readability.spotlight.help')
382
+ }
383
+ items={switches.map((id) => ({
384
+ label: t(`ui.readability.spotlight.${id}`),
385
+ help: t(`ui.readability.spotlight.${id}.help`),
386
+ }))}
387
+ />
388
+ }
389
+ >
390
+ <Segment
391
+ options={switches}
392
+ value={spotlight ? 'on' : 'off'}
393
+ onChange={(id) => setSpotlight(id === 'on')}
394
+ disabled={spotlightDisabled}
395
+ itemContent={(id) => t(`ui.readability.spotlight.${id}`)}
396
+ itemLabel={(id) => t(`ui.readability.spotlight.${id}`)}
397
+ itemHelp={(id) => t(`ui.readability.spotlight.${id}.help`)}
398
+ itemClassName="text-xs"
399
+ />
400
+ </ReadabilityField>
401
+ {spotlight && !spotlightDisabled && (
402
+ <ReadabilityField
403
+ label={t('ui.readability.spotlight.styles')}
404
+ icon={<Palette />}
405
+ help={
406
+ <ReadabilityHelp
407
+ title={t('ui.readability.spotlight.styles')}
408
+ description={t('ui.readability.spotlight.styles.help')}
409
+ items={spotlightStyles.map((id) => ({
410
+ icon: styleIcons[id],
411
+ label: t(`ui.readability.spotlight.${id}`),
412
+ help: t(`ui.readability.spotlight.${id}.help`),
413
+ }))}
414
+ />
415
+ }
416
+ >
417
+ <Segment
418
+ options={spotlightStyles}
419
+ value={spotlightStyle}
420
+ onChange={setSpotlightStyle}
421
+ disabled={spotlightDisabled || !spotlight}
422
+ itemContent={(id) => styleIcons[id]}
423
+ itemLabel={(id) => t(`ui.readability.spotlight.${id}`)}
424
+ itemHelp={(id) => t(`ui.readability.spotlight.${id}.help`)}
425
+ />
426
+ </ReadabilityField>
427
+ )}
428
+ </PopoverContent>
429
+ </Popover>
430
+ );
431
+ }