ember-joinery 1.0.1 → 1.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.
@@ -0,0 +1,86 @@
1
+ import { hash } from '@ember/helper'
2
+ import type { TOC } from '@ember/component/template-only'
3
+
4
+ export interface StatusBarItemSignature {
5
+ Args: {
6
+ /**
7
+ * Whether assistive technology announces a change of its text (`role="status"`, a polite
8
+ * live region), for a message such as "Saved". Default `false`.
9
+ */
10
+ live?: boolean
11
+ }
12
+ Blocks: { default: [] }
13
+ Element: HTMLDivElement
14
+ }
15
+
16
+ const Item: TOC<StatusBarItemSignature> = <template>
17
+ <div role={{if @live "status"}} data-jn-status-bar-item ...attributes>{{yield}}</div>
18
+ </template>
19
+
20
+ export interface StatusBarPartSignature {
21
+ Blocks: { default: [] }
22
+ Element: HTMLDivElement
23
+ }
24
+
25
+ const Leading: TOC<StatusBarPartSignature> = <template>
26
+ <div data-jn-status-bar-leading ...attributes>{{yield}}</div>
27
+ </template>
28
+
29
+ const Center: TOC<StatusBarPartSignature> = <template>
30
+ <div data-jn-status-bar-center ...attributes>{{yield}}</div>
31
+ </template>
32
+
33
+ const Trailing: TOC<StatusBarPartSignature> = <template>
34
+ <div data-jn-status-bar-trailing ...attributes>{{yield}}</div>
35
+ </template>
36
+
37
+ /** What a status bar yields: its parts. */
38
+ export interface StatusBarParts {
39
+ /** The inline start: the state of the document. */
40
+ Leading: typeof Leading
41
+ /** The middle. */
42
+ Center: typeof Leading
43
+ /** The inline end: the position, the encoding, a zoom control. */
44
+ Trailing: typeof Leading
45
+ /** One piece of status. `@live={{true}}` makes it a polite live region. */
46
+ Item: typeof Item
47
+ }
48
+
49
+ export interface StatusBarSignature {
50
+ Args: {
51
+ /** Its name, for `StatusBarHarness` (`data-test-jn-name`). */
52
+ name?: string
53
+ }
54
+ Blocks: { default: [StatusBarParts] }
55
+ Element: HTMLElement
56
+ }
57
+
58
+ /**
59
+ * The bar at the bottom of a window: 1.5rem tall, with small muted text. Joinery's own
60
+ * (shadcn/ui has none).
61
+ *
62
+ * <StatusBar as |bar|>
63
+ * <bar.Leading><bar.Item @live={{true}}>Saved</bar.Item></bar.Leading>
64
+ * <bar.Trailing><bar.Item>Line 12</bar.Item></bar.Trailing>
65
+ * </StatusBar>
66
+ *
67
+ * It is a material (`bar`) with its line at the top, and it keeps clear of `--jn-safe-bottom`.
68
+ * `--status-bar-height` sets its height. Change its edge with the `data-scroll-edge` attribute
69
+ * (`soft`, or `none`). It wraps the app's controls, so it is a wrapper for the harnesses
70
+ * (`data-test-jn-wrapper`).
71
+ */
72
+ const StatusBar: TOC<StatusBarSignature> = <template>
73
+ <footer
74
+ class="jn-material"
75
+ data-material="bar"
76
+ data-scroll-edge="hard"
77
+ data-scroll-edge-side="top"
78
+ data-jn-status-bar
79
+ data-test-jn="status-bar"
80
+ data-test-jn-wrapper
81
+ data-test-jn-name={{@name}}
82
+ ...attributes
83
+ >{{yield (hash Leading=Leading Center=Center Trailing=Trailing Item=Item)}}</footer>
84
+ </template>
85
+
86
+ export default StatusBar
@@ -0,0 +1,272 @@
1
+ import Component from '@glimmer/component'
2
+ import { hash } from '@ember/helper'
3
+ import type { TOC } from '@ember/component/template-only'
4
+ import type { WithBoundArgs } from '@glint/template'
5
+ import { Search as SearchIcon } from 'lucide'
6
+ import { As, type TagName } from '@ember-joinery/primitives/as'
7
+ import { controllable } from '@ember-joinery/primitives/controllable-state'
8
+ import { t } from '@ember-joinery/primitives/i18n'
9
+ import { part, type PartModifier } from '@ember-joinery/primitives/part'
10
+ import Icon from './icon.gts'
11
+ import { messages } from '../messages.ts'
12
+
13
+ /** `bar` is attached to the edge, full width. `floating` is a capsule inset from the edges. */
14
+ export type TabBarVariant = 'bar' | 'floating'
15
+
16
+ /** The edge the bar sticks to. A `top` bar is always the `floating` capsule, centred. */
17
+ export type TabBarPlacement = 'bottom' | 'top'
18
+
19
+ /** How far the scroll container must move before the bar changes, in CSS pixels. */
20
+ const SCROLL_THRESHOLD = 8
21
+
22
+ /** The `item` modifier: the classes and attributes of an item, for an element of your own. */
23
+ const item = part<HTMLElement, { current?: boolean }>(named => ({
24
+ class: 'jn-tab-bar-item',
25
+ attributes: {
26
+ 'data-slot': 'tab-bar-item',
27
+ 'data-test-jn-part': 'item',
28
+ // Without `current`, the element keeps its own `aria-current`.
29
+ 'aria-current': named.current ? 'page' : undefined,
30
+ },
31
+ }))
32
+
33
+ export interface TabBarItemSignature {
34
+ Args: {
35
+ /** Where the link goes. */
36
+ href?: string
37
+ /** Whether it is the section the user is in: it gets `aria-current="page"`. */
38
+ current?: boolean
39
+ /**
40
+ * A count (a number or a string) or, with `true`, a dot. `0` and `''` show nothing. Assistive
41
+ * technology hears `@badgeLabel`.
42
+ */
43
+ badge?: string | number | boolean
44
+ /** What the badge says. Default `3 new` for a count, `New` for a dot. */
45
+ badgeLabel?: string
46
+ /** The tag to render instead of `a`, such as `button`. */
47
+ as?: TagName
48
+ }
49
+ Blocks: {
50
+ /** An icon (an `<svg>`), then the label in a `<span>`. */
51
+ default: []
52
+ }
53
+ Element: HTMLAnchorElement
54
+ }
55
+
56
+ const isDot = (badge: unknown): boolean => badge === true
57
+
58
+ const Item: TOC<TabBarItemSignature> = <template>
59
+ <As
60
+ @as={{@as}}
61
+ @tag="a"
62
+ href={{@href}}
63
+ aria-current={{if @current "page"}}
64
+ class="jn-tab-bar-item"
65
+ data-slot="tab-bar-item"
66
+ data-test-jn-part="item"
67
+ ...attributes
68
+ >
69
+ {{yield}}
70
+ {{#if @badge}}
71
+ <span
72
+ class="jn-tab-bar-badge"
73
+ data-dot={{if (isDot @badge) ""}}
74
+ aria-hidden="true"
75
+ data-test-jn-part="badge"
76
+ >{{unless (isDot @badge) @badge}}</span>
77
+ <span class="jn-tab-bar-badge-label" data-test-jn-part="badge-label">{{if
78
+ @badgeLabel
79
+ @badgeLabel
80
+ (if
81
+ (isDot @badge) (t messages.tabBarBadgeDot) (t messages.tabBarBadge (hash count=@badge))
82
+ )
83
+ }}</span>
84
+ {{/if}}
85
+ </As>
86
+ </template>
87
+
88
+ export interface TabBarSearchSignature {
89
+ Args: {
90
+ /** Where the link goes. Without it, the search is a `<button type="button">`. */
91
+ href?: string
92
+ /** Its name for assistive technology. Default `Search`. */
93
+ label?: string
94
+ /** Whether the user is in the search section: it gets `aria-current="page"`. */
95
+ current?: boolean
96
+ /** The tag to render instead of `a` or `button`. */
97
+ as?: TagName
98
+ /** Set by the bar: whether the search is a capsule of its own, and so a material. */
99
+ floating: boolean
100
+ }
101
+ Element: HTMLElement
102
+ }
103
+
104
+ const searchTag = (as: TagName | undefined, href: string | undefined): TagName =>
105
+ as ?? (href ? 'a' : 'button')
106
+
107
+ const buttonType = (tag: TagName): string | undefined => (tag === 'button' ? 'button' : undefined)
108
+
109
+ const Search: TOC<TabBarSearchSignature> = <template>
110
+ {{#let (searchTag @as @href) as |tag|}}
111
+ <As
112
+ @as={{tag}}
113
+ @tag="a"
114
+ href={{@href}}
115
+ type={{buttonType tag}}
116
+ aria-label={{if @label @label (t messages.tabBarSearch)}}
117
+ aria-current={{if @current "page"}}
118
+ class="jn-tab-bar-search {{if @floating 'jn-material'}}"
119
+ data-material="bar"
120
+ data-slot="tab-bar-search"
121
+ data-test-jn-part="search"
122
+ ...attributes
123
+ ><Icon @icon={{SearchIcon}} /></As>
124
+ {{/let}}
125
+ </template>
126
+
127
+ export interface TabBarAccessorySignature {
128
+ Args: {
129
+ /** The tag to render instead of `div`. */
130
+ as?: TagName
131
+ }
132
+ Blocks: { default: [] }
133
+ Element: HTMLElement
134
+ }
135
+
136
+ const Accessory: TOC<TabBarAccessorySignature> = <template>
137
+ <As
138
+ @as={{@as}}
139
+ class="jn-tab-bar-accessory jn-material"
140
+ data-material="bar"
141
+ data-slot="tab-bar-accessory"
142
+ data-test-jn-part="accessory"
143
+ ...attributes
144
+ >{{yield}}</As>
145
+ </template>
146
+
147
+ /** What a tab bar yields: its parts. */
148
+ export interface TabBarParts {
149
+ /** One section of the app: an `<a>` with an icon over a label. */
150
+ Item: typeof Item
151
+ /**
152
+ * Makes your own element an item (`<LinkTo @route="inbox" {{bar.item}}>`). The `active` class
153
+ * or `aria-current="page"` shows it as the current item; `current=true` sets `aria-current`.
154
+ */
155
+ item: PartModifier<HTMLElement, { current?: boolean }>
156
+ /** A round search item at the inline end. In `floating`, a capsule of its own. */
157
+ Search: WithBoundArgs<typeof Search, 'floating'>
158
+ /** A strip above the bar, such as a mini player. */
159
+ Accessory: typeof Accessory
160
+ }
161
+
162
+ export interface TabBarSignature {
163
+ Args: {
164
+ /** The name of the `<nav>` for assistive technology. Default `Tabs`. */
165
+ label?: string
166
+ /** Default `bar`. `floating` is a capsule above the bottom safe area. */
167
+ variant?: TabBarVariant
168
+ /** Default `bottom`. `top` is a centred capsule below the top safe area. */
169
+ placement?: TabBarPlacement
170
+ /**
171
+ * Whether a scroll down minimizes the bar and a scroll up restores it. It listens to the
172
+ * nearest scroll container around the bar, or the window. Default `false`.
173
+ */
174
+ minimizeOnScroll?: boolean
175
+ /** The scroll container to listen to, when the bar is not inside it. */
176
+ scrollElement?: Element | null
177
+ /** Whether the bar is minimized, controlled. */
178
+ minimized?: boolean
179
+ /** Called with each new minimized state. */
180
+ onMinimizedChange?: (minimized: boolean) => void
181
+ /** Its name, for `TabBarHarness` (`data-test-jn-name`). */
182
+ name?: string
183
+ }
184
+ Blocks: { default: [TabBarParts] }
185
+ Element: HTMLElement
186
+ }
187
+
188
+ /**
189
+ * The tab bar of a phone or tablet app: links to the top-level sections. Joinery's own (shadcn/ui
190
+ * has none). It is navigation, so it is a `<nav>` of links, not a `tablist`: the current item has
191
+ * `aria-current="page"`.
192
+ *
193
+ * <TabBar as |bar|>
194
+ * <bar.Item @href="/" @current={{true}}><Icon @icon={{House}} /><span>Home</span></bar.Item>
195
+ * <LinkTo @route="inbox" {{bar.item}}><Icon @icon={{Inbox}} /><span>Inbox</span></LinkTo>
196
+ * <bar.Search @href="/search" />
197
+ * </TabBar>
198
+ *
199
+ * The bar sticks to its edge and keeps clear of the safe areas. Its surface is a material. In an
200
+ * app shell's `Footer`, set `--app-shell-footer-height` to `3.0625rem` (`4rem` for `floating`)
201
+ * and give the footer `@material={{false}} @scrollEdge="none"`.
202
+ */
203
+ export default class TabBar extends Component<TabBarSignature> {
204
+ minimized = controllable(this, 'minimized', false)
205
+
206
+ /** The last scroll position of each scroll container. */
207
+ #last = new WeakMap<object, number>()
208
+
209
+ /** One passive listener hears every scroll in the document: a scroll event does not bubble. */
210
+ root = part<HTMLElement>(() => ({
211
+ element: element => {
212
+ const doc = element.ownerDocument
213
+ const scrolled = (event: Event): void => this.#scrolled(element, event)
214
+ doc.addEventListener('scroll', scrolled, { capture: true, passive: true })
215
+ return () => doc.removeEventListener('scroll', scrolled, { capture: true })
216
+ },
217
+ }))
218
+
219
+ get placement(): TabBarPlacement {
220
+ return this.args.placement ?? 'bottom'
221
+ }
222
+
223
+ get variant(): TabBarVariant {
224
+ return this.placement === 'top' ? 'floating' : (this.args.variant ?? 'bar')
225
+ }
226
+
227
+ get floating(): boolean {
228
+ return this.variant === 'floating'
229
+ }
230
+
231
+ #scrolled(root: HTMLElement, event: Event): void {
232
+ if (!this.args.minimizeOnScroll) return
233
+ const target = event.target as Document | Element
234
+ const wanted = this.args.scrollElement
235
+ if (wanted ? target !== wanted : !target.contains(root)) return
236
+ const top =
237
+ 'scrollingElement' in target ? (target.scrollingElement?.scrollTop ?? 0) : target.scrollTop
238
+ const known = this.#last.has(target)
239
+ const last = this.#last.get(target) ?? top
240
+ if (known && Math.abs(top - last) < SCROLL_THRESHOLD) return
241
+ this.#last.set(target, top)
242
+ if (top !== last) this.minimized.set(top > last && top > 0)
243
+ }
244
+
245
+ <template>
246
+ <nav
247
+ aria-label={{if @label @label (t messages.tabBarLabel)}}
248
+ class="jn-tab-bar"
249
+ data-slot="tab-bar"
250
+ data-variant={{this.variant}}
251
+ data-placement={{this.placement}}
252
+ data-minimized={{if this.minimized.value ""}}
253
+ data-test-jn="tab-bar"
254
+ data-test-jn-name={{@name}}
255
+ ...attributes
256
+ {{this.root}}
257
+ >
258
+ <div
259
+ class="jn-tab-bar-surface jn-material"
260
+ data-material="bar"
261
+ data-scroll-edge={{unless this.floating "soft"}}
262
+ data-scroll-edge-side="top"
263
+ data-slot="tab-bar-surface"
264
+ data-test-jn-part="surface"
265
+ >{{yield
266
+ (hash
267
+ Item=Item item=item Search=(component Search floating=this.floating) Accessory=Accessory
268
+ )
269
+ }}</div>
270
+ </nav>
271
+ </template>
272
+ }
@@ -0,0 +1,193 @@
1
+ import Component from '@glimmer/component'
2
+ import { tracked } from '@glimmer/tracking'
3
+ import { hash } from '@ember/helper'
4
+ import { registerDestructor } from '@ember/destroyable'
5
+ import type Owner from '@ember/owner'
6
+ import type { TOC } from '@ember/component/template-only'
7
+ import { As, type TagName } from '@ember-joinery/primitives/as'
8
+
9
+ /**
10
+ * How the bar is laid out, as a macOS window toolbar is: `unified` is one row 3.25rem tall,
11
+ * `unified-compact` is one row 2.375rem tall, and `expanded` has a toolbar row under the title.
12
+ */
13
+ export type TitleBarVariant = 'unified' | 'unified-compact' | 'expanded'
14
+
15
+ /**
16
+ * Where the window has its own controls: `start` keeps room for the macOS traffic lights, and
17
+ * `overlay` puts the bar in the title bar area of an installed desktop PWA.
18
+ */
19
+ export type TitleBarControls = 'none' | 'start' | 'overlay'
20
+
21
+ /** What the bar draws where content scrolls under it: a line, a fade, or nothing. */
22
+ export type TitleBarScrollEdge = 'hard' | 'soft' | 'none'
23
+
24
+ export interface TitleBarTitleSignature {
25
+ Args: {
26
+ /** The tag to render instead of `h1`: `h2` for a window inside a page. */
27
+ as?: TagName
28
+ /** A second line under the title, such as `3 messages`. */
29
+ subtitle?: string
30
+ }
31
+ Blocks: { default: [] }
32
+ Element: HTMLElement
33
+ }
34
+
35
+ const Title: TOC<TitleBarTitleSignature> = <template>
36
+ <div data-jn-title-bar-heading>
37
+ <As @as={{if @as @as "h1"}} data-jn-title-bar-title ...attributes>{{yield}}</As>
38
+ {{#if @subtitle}}
39
+ <p data-jn-title-bar-subtitle>{{@subtitle}}</p>
40
+ {{/if}}
41
+ </div>
42
+ </template>
43
+
44
+ export interface TitleBarTrafficLightsSignature {
45
+ Element: HTMLSpanElement
46
+ }
47
+
48
+ /**
49
+ * Three decorative circles in the place of the macOS window controls. Use it only for a mock
50
+ * window in a document or a marketing page. A real app uses the native controls: keep room for
51
+ * them with `@controls`.
52
+ */
53
+ const TrafficLights: TOC<TitleBarTrafficLightsSignature> = <template>
54
+ <span aria-hidden="true" data-jn-title-bar-traffic-lights ...attributes><span
55
+ data-light="close"
56
+ ></span><span data-light="minimize"></span><span data-light="zoom"></span></span>
57
+ </template>
58
+
59
+ export interface TitleBarPartSignature {
60
+ Blocks: { default: [] }
61
+ Element: HTMLDivElement
62
+ }
63
+
64
+ const Leading: TOC<TitleBarPartSignature> = <template>
65
+ <div data-jn-title-bar-leading ...attributes>{{yield}}</div>
66
+ </template>
67
+
68
+ const Center: TOC<TitleBarPartSignature> = <template>
69
+ <div data-jn-title-bar-center ...attributes>{{yield}}</div>
70
+ </template>
71
+
72
+ const Trailing: TOC<TitleBarPartSignature> = <template>
73
+ <div data-jn-title-bar-trailing ...attributes>{{yield}}</div>
74
+ </template>
75
+
76
+ const ToolbarRow: TOC<TitleBarPartSignature> = <template>
77
+ <div data-jn-title-bar-toolbar ...attributes>{{yield}}</div>
78
+ </template>
79
+
80
+ /** What a title bar yields: its parts. Mark a part `data-no-drag` to take it from the drag region. */
81
+ export interface TitleBarParts {
82
+ /** The inline start: a sidebar toggle, back and forward. */
83
+ Leading: typeof Leading
84
+ /** The window's title: an `<h1>`, or `@as`. `@subtitle` adds a second line. */
85
+ Title: typeof Title
86
+ /** The middle: the window's main controls, or the tabs of a settings window. */
87
+ Center: typeof Leading
88
+ /** The inline end: search, share, an inspector toggle. */
89
+ Trailing: typeof Leading
90
+ /** A row of its own under the title row, for the `expanded` variant. Put a `<Toolbar>` in it. */
91
+ Toolbar: typeof Leading
92
+ /** Decorative window controls for a mock window. A real app uses the native controls. */
93
+ TrafficLights: typeof TrafficLights
94
+ }
95
+
96
+ export interface TitleBarSignature {
97
+ Args: {
98
+ /** Default `unified`: one row, 3.25rem. `unified-compact`: 2.375rem. `expanded`: two rows. */
99
+ variant?: TitleBarVariant
100
+ /**
101
+ * Default `none`. `start` pads the inline start by `--jn-window-controls-inset` (78px when
102
+ * the app sets none), for the macOS traffic lights. `overlay` places and sizes the bar with
103
+ * the `--jn-titlebar-*` tokens, for the Window Controls Overlay of an installed desktop PWA.
104
+ */
105
+ controls?: TitleBarControls
106
+ /**
107
+ * Whether the bar moves the window when the user drags it (`app-region: drag`). Default
108
+ * `true`. Its controls, and each part marked `data-no-drag`, do not. A browser tab ignores it.
109
+ */
110
+ draggable?: boolean
111
+ /**
112
+ * Forces the inactive look (`true`) or the active look (`false`). When you do not set it,
113
+ * the bar is inactive while the window has no focus.
114
+ */
115
+ inactive?: boolean
116
+ /** What it draws where content scrolls under it. Default `hard`, a line. */
117
+ scrollEdge?: TitleBarScrollEdge
118
+ /** Its name, for `TitleBarHarness` (`data-test-jn-name`). */
119
+ name?: string
120
+ }
121
+ Blocks: { default: [TitleBarParts] }
122
+ Element: HTMLElement
123
+ }
124
+
125
+ const edge = (value: TitleBarScrollEdge | undefined): string | undefined =>
126
+ value === 'none' ? undefined : (value ?? 'hard')
127
+
128
+ const isFalse = (value: unknown): boolean => value === false
129
+
130
+ /**
131
+ * The bar at the top of a window: the macOS window toolbar, in the web's terms. Joinery's own
132
+ * (shadcn/ui has none). It is for a macOS style application shell, for a desktop PWA with the
133
+ * Window Controls Overlay, and for an Electron or Tauri app with a hidden native title bar.
134
+ *
135
+ * <TitleBar @controls="start" as |bar|>
136
+ * <bar.Leading><Button @variant="ghost" @size="icon-sm" aria-label="Back">…</Button></bar.Leading>
137
+ * <bar.Title @subtitle="3 messages">Inbox</bar.Title>
138
+ * <bar.Trailing><Input type="search" aria-label="Search" /></bar.Trailing>
139
+ * </TitleBar>
140
+ *
141
+ * The bar is a drag region, a material (`bar`), and it keeps clear of `--jn-safe-top`.
142
+ * `--title-bar-height` sets the height of its row. It wraps the app's controls, so it is a
143
+ * wrapper for the harnesses (`data-test-jn-wrapper`): a part of a component around it, such as
144
+ * a tab list or a sidebar trigger, still belongs to that component.
145
+ */
146
+ export default class TitleBar extends Component<TitleBarSignature> {
147
+ // No document on a server: the bar renders active there.
148
+ @tracked windowInactive = (globalThis.document as Document | undefined)?.hasFocus() === false
149
+
150
+ constructor(owner: Owner, args: TitleBarSignature['Args']) {
151
+ super(owner, args)
152
+ const view = globalThis.window as Window | undefined
153
+ if (!view) return
154
+ const blur = () => (this.windowInactive = true)
155
+ const focus = () => (this.windowInactive = false)
156
+ view.addEventListener('blur', blur)
157
+ view.addEventListener('focus', focus)
158
+ registerDestructor(this, () => {
159
+ view.removeEventListener('blur', blur)
160
+ view.removeEventListener('focus', focus)
161
+ })
162
+ }
163
+
164
+ get inactive(): boolean {
165
+ return this.args.inactive ?? this.windowInactive
166
+ }
167
+
168
+ <template>
169
+ <header
170
+ class="jn-material"
171
+ data-material="bar"
172
+ data-scroll-edge={{edge @scrollEdge}}
173
+ data-jn-title-bar
174
+ data-variant={{if @variant @variant "unified"}}
175
+ data-controls={{if @controls @controls "none"}}
176
+ data-draggable={{unless (isFalse @draggable) ""}}
177
+ data-inactive={{if this.inactive ""}}
178
+ data-test-jn="title-bar"
179
+ data-test-jn-wrapper
180
+ data-test-jn-name={{@name}}
181
+ ...attributes
182
+ >{{yield
183
+ (hash
184
+ Leading=Leading
185
+ Title=Title
186
+ Center=Center
187
+ Trailing=Trailing
188
+ Toolbar=ToolbarRow
189
+ TrafficLights=TrafficLights
190
+ )
191
+ }}</header>
192
+ </template>
193
+ }