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.
- package/CHANGELOG.md +4 -0
- package/package.json +2 -2
- package/registry/files/components/app-shell.gts +221 -0
- package/registry/files/components/inspector.gts +459 -0
- package/registry/files/components/navigation-bar.gts +101 -0
- package/registry/files/components/navigation-stack.gts +305 -0
- package/registry/files/components/page.gts +134 -0
- package/registry/files/components/pull-to-refresh.gts +227 -0
- package/registry/files/components/split-view.gts +648 -0
- package/registry/files/components/status-bar.gts +86 -0
- package/registry/files/components/tab-bar.gts +272 -0
- package/registry/files/components/title-bar.gts +193 -0
- package/registry/files/components/toolbar.gts +174 -0
- package/registry/files/components/window-frame.gts +120 -0
- package/registry/files/messages.ts +70 -0
- package/registry/manifest.json +138 -2
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
import { isNumber, isUndefined } from '@sindresorhus/is'
|
|
2
|
+
import Component from '@glimmer/component'
|
|
3
|
+
import { cached, tracked } from '@glimmer/tracking'
|
|
4
|
+
import { hash } from '@ember/helper'
|
|
5
|
+
import { on } from '@ember/modifier'
|
|
6
|
+
import type { TOC } from '@ember/component/template-only'
|
|
7
|
+
import type { WithBoundArgs } from '@glint/template'
|
|
8
|
+
import { ChevronLeft, PanelLeft } from 'lucide'
|
|
9
|
+
import { As, type TagName } from '@ember-joinery/primitives/as'
|
|
10
|
+
import { controllable } from '@ember-joinery/primitives/controllable-state'
|
|
11
|
+
import { idFor } from '@ember-joinery/primitives/id'
|
|
12
|
+
import { t } from '@ember-joinery/primitives/i18n'
|
|
13
|
+
import { part, type PartModifier, type PartSpec } from '@ember-joinery/primitives/part'
|
|
14
|
+
import {
|
|
15
|
+
ResizableGroup,
|
|
16
|
+
type ResizableLayoutMap,
|
|
17
|
+
type ResizableLayoutMeta,
|
|
18
|
+
} from '@ember-joinery/primitives/resizable'
|
|
19
|
+
import { visuallyHidden } from '@ember-joinery/primitives/visually-hidden'
|
|
20
|
+
import Button from './button.gts'
|
|
21
|
+
import Icon, { type IconNode } from './icon.gts'
|
|
22
|
+
import { messages } from '../messages.ts'
|
|
23
|
+
|
|
24
|
+
/** The cookie the split view keeps its visibility and its column widths in. */
|
|
25
|
+
export const SPLIT_VIEW_COOKIE_NAME = 'split_view_state'
|
|
26
|
+
/** How long the cookie lasts, in seconds: a week, as the sidebar's. */
|
|
27
|
+
export const SPLIT_VIEW_COOKIE_MAX_AGE = 60 * 60 * 24 * 7
|
|
28
|
+
/** The width in pixels below which a split view shows one column at a time. */
|
|
29
|
+
export const SPLIT_VIEW_COMPACT_BREAKPOINT = 768
|
|
30
|
+
|
|
31
|
+
/** Which columns a wide view shows, as SwiftUI's `NavigationSplitViewVisibility`. */
|
|
32
|
+
export type SplitViewVisibility = 'all' | 'double-column' | 'detail-only'
|
|
33
|
+
|
|
34
|
+
/** A column of a split view. */
|
|
35
|
+
export type SplitViewColumn = 'sidebar' | 'content' | 'detail'
|
|
36
|
+
|
|
37
|
+
/** How the sidebar shows in a view that is too narrow for its columns side by side. */
|
|
38
|
+
export type SplitViewMode = 'tile' | 'overlay' | 'displace'
|
|
39
|
+
|
|
40
|
+
/** What the cookie holds: the visibility, and the widths of the leading columns in pixels. */
|
|
41
|
+
export interface SplitViewState {
|
|
42
|
+
visibility?: SplitViewVisibility
|
|
43
|
+
sidebar?: number
|
|
44
|
+
content?: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const VISIBILITIES: readonly unknown[] = ['all', 'double-column', 'detail-only']
|
|
48
|
+
|
|
49
|
+
/** The widths of the leading columns in pixels, and the width the detail needs. */
|
|
50
|
+
const WIDTHS = {
|
|
51
|
+
sidebar: { min: 180, default: 256, max: 400 },
|
|
52
|
+
content: { min: 240, default: 320, max: 480 },
|
|
53
|
+
detail: { min: 320 },
|
|
54
|
+
} as const
|
|
55
|
+
|
|
56
|
+
const isWidth = (value: unknown): value is number =>
|
|
57
|
+
isNumber(value) && Number.isFinite(value) && value > 0
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The state that the split view keeps in its cookie each time the user changes it. An empty
|
|
61
|
+
* object when there is no cookie. Give its values back to the view:
|
|
62
|
+
*
|
|
63
|
+
* const saved = splitViewStateFromCookie()
|
|
64
|
+
*
|
|
65
|
+
* <SplitView @defaultVisibility={{saved.visibility}} as |view|>
|
|
66
|
+
* <view.Sidebar @defaultWidth={{saved.sidebar}}>…</view.Sidebar>
|
|
67
|
+
*/
|
|
68
|
+
export function splitViewStateFromCookie(
|
|
69
|
+
cookieName: string = SPLIT_VIEW_COOKIE_NAME,
|
|
70
|
+
doc: Document | undefined = isUndefined(globalThis.document) ? undefined : document,
|
|
71
|
+
): SplitViewState {
|
|
72
|
+
const prefix = `${cookieName}=`
|
|
73
|
+
const entry = doc?.cookie
|
|
74
|
+
.split(';')
|
|
75
|
+
.map(cookie => cookie.trim())
|
|
76
|
+
.find(cookie => cookie.startsWith(prefix))
|
|
77
|
+
if (entry === undefined) return {}
|
|
78
|
+
let saved: Record<string, unknown>
|
|
79
|
+
try {
|
|
80
|
+
saved = JSON.parse(decodeURIComponent(entry.slice(prefix.length))) as Record<string, unknown>
|
|
81
|
+
} catch {
|
|
82
|
+
return {}
|
|
83
|
+
}
|
|
84
|
+
const state: SplitViewState = {}
|
|
85
|
+
if (VISIBILITIES.includes(saved?.['visibility'])) {
|
|
86
|
+
state.visibility = saved['visibility'] as SplitViewVisibility
|
|
87
|
+
}
|
|
88
|
+
if (isWidth(saved?.['sidebar'])) state.sidebar = saved['sidebar']
|
|
89
|
+
if (isWidth(saved?.['content'])) state.content = saved['content']
|
|
90
|
+
return state
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const materialClass = (material: boolean | undefined): string | undefined =>
|
|
94
|
+
material ? 'jn-material' : undefined
|
|
95
|
+
|
|
96
|
+
const isSidebar = (column: SplitViewColumn): boolean => column === 'sidebar'
|
|
97
|
+
|
|
98
|
+
const or = (value: number | undefined, fallback: number): number => value ?? fallback
|
|
99
|
+
|
|
100
|
+
interface NeedSignature {
|
|
101
|
+
Args: { view: SplitView; column: 'sidebar' | 'content'; width: number }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Tells the view the width that a leading column needs. When the app stops rendering the
|
|
106
|
+
* column, it tells the view that the column needs no width, so the view has no such column.
|
|
107
|
+
*/
|
|
108
|
+
class Need extends Component<NeedSignature> {
|
|
109
|
+
willDestroy(): void {
|
|
110
|
+
super.willDestroy()
|
|
111
|
+
this.args.view.note(this.args.column, 0)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
<template>{{@view.note @column @width}}</template>
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface SplitViewLeadingSignature {
|
|
118
|
+
Args: {
|
|
119
|
+
/** Its name for assistive technology. Default `Sidebar`, or `Content` for the middle column. */
|
|
120
|
+
label?: string
|
|
121
|
+
/** Its smallest width in pixels. Default 180 for the sidebar, 240 for the content. */
|
|
122
|
+
minWidth?: number
|
|
123
|
+
/** Its first width in pixels. Default 256 for the sidebar, 320 for the content. */
|
|
124
|
+
defaultWidth?: number
|
|
125
|
+
/** Its largest width in pixels. Default 400 for the sidebar, 480 for the content. */
|
|
126
|
+
maxWidth?: number
|
|
127
|
+
/** Whether it is a material (`jn-material`, the `sidebar` surface). Default `false`. */
|
|
128
|
+
material?: boolean
|
|
129
|
+
/** The tag to render. Default `section`. */
|
|
130
|
+
as?: TagName
|
|
131
|
+
/** @internal Bound by the view. */
|
|
132
|
+
view: SplitView
|
|
133
|
+
/** @internal Bound by the view. */
|
|
134
|
+
column: 'sidebar' | 'content'
|
|
135
|
+
}
|
|
136
|
+
Blocks: {
|
|
137
|
+
/** What the column holds. It scrolls when it does not fit. */
|
|
138
|
+
default: []
|
|
139
|
+
}
|
|
140
|
+
Element: HTMLElement
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A leading column: the sidebar or the content. A tiled column is a panel of the view's
|
|
145
|
+
* resizable group, followed by its separator. An overlaid sidebar is not a panel, so the other
|
|
146
|
+
* columns keep their shares.
|
|
147
|
+
*/
|
|
148
|
+
const Leading: TOC<SplitViewLeadingSignature> = <template>
|
|
149
|
+
{{#let (widthsOf @column) as |widths|}}
|
|
150
|
+
<Need @view={{@view}} @column={{@column}} @width={{or @defaultWidth widths.default}} />
|
|
151
|
+
{{#if (@view.shows @column)}}
|
|
152
|
+
{{#if (@view.floats @column)}}
|
|
153
|
+
<As
|
|
154
|
+
@as={{if @as @as "section"}}
|
|
155
|
+
id={{@view.columnId @column}}
|
|
156
|
+
class={{materialClass @material}}
|
|
157
|
+
data-material="sidebar"
|
|
158
|
+
data-material-blending={{if (isSidebar @column) "behind-window"}}
|
|
159
|
+
aria-label={{if @label @label (t (columnLabel @column))}}
|
|
160
|
+
tabindex="-1"
|
|
161
|
+
data-jn-split-view-column={{@column}}
|
|
162
|
+
data-floating
|
|
163
|
+
...attributes
|
|
164
|
+
{{@view.floating}}
|
|
165
|
+
><div data-jn-split-view-scroller>{{yield}}</div></As>
|
|
166
|
+
{{else}}
|
|
167
|
+
<As
|
|
168
|
+
@as={{if @as @as "section"}}
|
|
169
|
+
class={{materialClass @material}}
|
|
170
|
+
data-material="sidebar"
|
|
171
|
+
data-material-blending={{if (isSidebar @column) "behind-window"}}
|
|
172
|
+
aria-label={{if @label @label (t (columnLabel @column))}}
|
|
173
|
+
data-jn-split-view-column={{@column}}
|
|
174
|
+
...attributes
|
|
175
|
+
{{@view.group.panel
|
|
176
|
+
id=(@view.columnId @column)
|
|
177
|
+
defaultSize=(@view.widthOf @column (or @defaultWidth widths.default))
|
|
178
|
+
minSize=(or @minWidth widths.min)
|
|
179
|
+
maxSize=(or @maxWidth widths.max)
|
|
180
|
+
groupResizeBehavior="preserve-pixel-size"
|
|
181
|
+
}}
|
|
182
|
+
><div data-jn-split-view-scroller>{{yield}}</div></As>
|
|
183
|
+
<div
|
|
184
|
+
aria-label={{t (resizeLabel @column)}}
|
|
185
|
+
data-jn-split-view-separator
|
|
186
|
+
{{@view.group.handle}}
|
|
187
|
+
></div>
|
|
188
|
+
{{/if}}
|
|
189
|
+
{{/if}}
|
|
190
|
+
{{/let}}
|
|
191
|
+
</template>
|
|
192
|
+
|
|
193
|
+
export interface SplitViewDetailSignature {
|
|
194
|
+
Args: {
|
|
195
|
+
/** Its name for assistive technology. Default `Detail`. */
|
|
196
|
+
label?: string
|
|
197
|
+
/**
|
|
198
|
+
* The width in pixels that it needs. A view narrower than this plus the default widths of
|
|
199
|
+
* the leading columns shows its sidebar as `@mode` says. Default 320.
|
|
200
|
+
*/
|
|
201
|
+
minWidth?: number
|
|
202
|
+
/** Whether it is a material (`jn-material`, the `sidebar` surface). Default `false`. */
|
|
203
|
+
material?: boolean
|
|
204
|
+
/** The tag to render. Default `section`. */
|
|
205
|
+
as?: TagName
|
|
206
|
+
/** @internal Bound by the view. */
|
|
207
|
+
view: SplitView
|
|
208
|
+
}
|
|
209
|
+
Blocks: {
|
|
210
|
+
/** The main content. It scrolls when it does not fit. */
|
|
211
|
+
default: []
|
|
212
|
+
}
|
|
213
|
+
Element: HTMLElement
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const Detail: TOC<SplitViewDetailSignature> = <template>
|
|
217
|
+
{{@view.note "detail" (or @minWidth WIDTHS.detail.min)}}
|
|
218
|
+
<As
|
|
219
|
+
@as={{if @as @as "section"}}
|
|
220
|
+
class={{materialClass @material}}
|
|
221
|
+
data-material="sidebar"
|
|
222
|
+
aria-label={{if @label @label (t messages.splitViewDetailLabel)}}
|
|
223
|
+
data-jn-split-view-column="detail"
|
|
224
|
+
...attributes
|
|
225
|
+
{{@view.group.panel id=(@view.columnId "detail")}}
|
|
226
|
+
><div data-jn-split-view-scroller>{{yield}}</div></As>
|
|
227
|
+
</template>
|
|
228
|
+
|
|
229
|
+
export interface SplitViewButtonSignature {
|
|
230
|
+
Args: {
|
|
231
|
+
/** Its name for assistive technology. Default `Toggle Sidebar`, or `Back`. */
|
|
232
|
+
label?: string
|
|
233
|
+
/** Its icon: one from `lucide`. Default `PanelLeft`, or `ChevronLeft`. */
|
|
234
|
+
icon?: IconNode
|
|
235
|
+
/** @internal Bound by the view. */
|
|
236
|
+
view: SplitView
|
|
237
|
+
}
|
|
238
|
+
/** A ghost, small icon Button. */
|
|
239
|
+
Element: HTMLButtonElement | HTMLAnchorElement
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A button that hides and shows the sidebar. A compact view does not render it. */
|
|
243
|
+
const SidebarToggle: TOC<SplitViewButtonSignature> = <template>
|
|
244
|
+
{{#unless @view.isCompact}}
|
|
245
|
+
<Button
|
|
246
|
+
@variant="ghost"
|
|
247
|
+
@size="icon-sm"
|
|
248
|
+
type="button"
|
|
249
|
+
aria-expanded={{if @view.isSidebarShown "true" "false"}}
|
|
250
|
+
aria-controls={{@view.columnId "sidebar"}}
|
|
251
|
+
data-jn-split-view-toggle
|
|
252
|
+
...attributes
|
|
253
|
+
{{on "click" @view.toggleSidebar}}
|
|
254
|
+
>
|
|
255
|
+
<Icon @icon={{if @icon @icon PanelLeft}} class="jn-rtl-flip" />
|
|
256
|
+
<span {{visuallyHidden}}>{{if @label @label (t messages.splitViewToggleSidebar)}}</span>
|
|
257
|
+
</Button>
|
|
258
|
+
{{/unless}}
|
|
259
|
+
</template>
|
|
260
|
+
|
|
261
|
+
/** A button that goes to the column before. Only a compact view past its first column renders it. */
|
|
262
|
+
const BackButton: TOC<SplitViewButtonSignature> = <template>
|
|
263
|
+
{{#if @view.canGoBack}}
|
|
264
|
+
<Button
|
|
265
|
+
@variant="ghost"
|
|
266
|
+
@size="icon-sm"
|
|
267
|
+
type="button"
|
|
268
|
+
data-jn-split-view-back
|
|
269
|
+
...attributes
|
|
270
|
+
{{on "click" @view.back}}
|
|
271
|
+
>
|
|
272
|
+
<Icon @icon={{if @icon @icon ChevronLeft}} class="jn-rtl-flip" />
|
|
273
|
+
<span {{visuallyHidden}}>{{if @label @label (t messages.splitViewBack)}}</span>
|
|
274
|
+
</Button>
|
|
275
|
+
{{/if}}
|
|
276
|
+
</template>
|
|
277
|
+
|
|
278
|
+
function widthsOf(column: 'sidebar' | 'content') {
|
|
279
|
+
return WIDTHS[column]
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function columnLabel(column: 'sidebar' | 'content') {
|
|
283
|
+
return column === 'sidebar' ? messages.splitViewSidebarLabel : messages.splitViewContentLabel
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function resizeLabel(column: 'sidebar' | 'content') {
|
|
287
|
+
return column === 'sidebar' ? messages.splitViewResizeSidebar : messages.splitViewResizeContent
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const isFalse = (value: unknown): boolean => value === false
|
|
291
|
+
|
|
292
|
+
const isOverlaid = (layout: string | undefined): boolean => layout === 'overlaid'
|
|
293
|
+
|
|
294
|
+
/** What a split view yields: its columns, its buttons and its state. */
|
|
295
|
+
export interface SplitViewParts {
|
|
296
|
+
/** The first column: a `<section>` named `Sidebar`. */
|
|
297
|
+
Sidebar: WithBoundArgs<typeof Leading, 'view' | 'column'>
|
|
298
|
+
/** The optional middle column, for a list: a `<section>` named `Content`. */
|
|
299
|
+
Content: WithBoundArgs<typeof Leading, 'view' | 'column'>
|
|
300
|
+
/** The last column, for the main content: a `<section>` named `Detail`. */
|
|
301
|
+
Detail: WithBoundArgs<typeof Detail, 'view'>
|
|
302
|
+
/** A button that hides and shows the sidebar. */
|
|
303
|
+
SidebarToggle: WithBoundArgs<typeof SidebarToggle, 'view'>
|
|
304
|
+
/** A button that goes to the column before, in a compact view. */
|
|
305
|
+
BackButton: WithBoundArgs<typeof BackButton, 'view'>
|
|
306
|
+
/** Which columns a wide view shows. */
|
|
307
|
+
visibility: SplitViewVisibility
|
|
308
|
+
/** Sets which columns a wide view shows. */
|
|
309
|
+
setVisibility: (visibility: SplitViewVisibility) => void
|
|
310
|
+
/** Hides or shows the sidebar. In a compact view, it shows the sidebar column. */
|
|
311
|
+
toggleSidebar: () => void
|
|
312
|
+
/** Whether the view is narrower than `@compactBreakpoint`, and shows one column at a time. */
|
|
313
|
+
isCompact: boolean
|
|
314
|
+
/** The column a compact view shows. */
|
|
315
|
+
compactColumn: SplitViewColumn
|
|
316
|
+
/** Sets the column a compact view shows. Call it when the user picks an item. */
|
|
317
|
+
showColumn: (column: SplitViewColumn) => void
|
|
318
|
+
/** Goes to the column before the one a compact view shows. */
|
|
319
|
+
back: () => void
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export interface SplitViewSignature {
|
|
323
|
+
Args: {
|
|
324
|
+
/** Which columns a wide view shows, controlled: `all`, `double-column` or `detail-only`. */
|
|
325
|
+
visibility?: SplitViewVisibility
|
|
326
|
+
/** The first visibility, uncontrolled. Default `all`. */
|
|
327
|
+
defaultVisibility?: SplitViewVisibility
|
|
328
|
+
/** Called with each new visibility. */
|
|
329
|
+
onVisibilityChange?: (visibility: SplitViewVisibility) => void
|
|
330
|
+
/**
|
|
331
|
+
* How the sidebar shows in a view that is too narrow for its columns side by side: `tile`
|
|
332
|
+
* (the default), `overlay` (over the detail) or `displace` (it pushes the detail).
|
|
333
|
+
*/
|
|
334
|
+
mode?: SplitViewMode
|
|
335
|
+
/** Whether the separators move. Default `true`. `false` gives fixed widths and a 1px line. */
|
|
336
|
+
resizable?: boolean
|
|
337
|
+
/** The width in pixels below which the view shows one column at a time. Default 768. */
|
|
338
|
+
compactBreakpoint?: number
|
|
339
|
+
/** The column a compact view shows, controlled. */
|
|
340
|
+
compactColumn?: SplitViewColumn
|
|
341
|
+
/** The first compact column, uncontrolled. Default `sidebar`. */
|
|
342
|
+
defaultCompactColumn?: SplitViewColumn
|
|
343
|
+
/** Called with each new compact column. */
|
|
344
|
+
onCompactColumnChange?: (column: SplitViewColumn) => void
|
|
345
|
+
/**
|
|
346
|
+
* The cookie for the visibility and the column widths. Default `split_view_state`; `false`
|
|
347
|
+
* keeps no cookie. Read it with `splitViewStateFromCookie()`.
|
|
348
|
+
*/
|
|
349
|
+
cookieName?: string | false
|
|
350
|
+
/** Its name, for `SplitViewHarness` (`data-test-jn-name`). */
|
|
351
|
+
name?: string
|
|
352
|
+
}
|
|
353
|
+
Blocks: { default: [SplitViewParts] }
|
|
354
|
+
Element: HTMLDivElement
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Two or three columns side by side: a sidebar, an optional content list and a detail. Joinery's
|
|
359
|
+
* own (shadcn/ui has no split view), after SwiftUI's `NavigationSplitView`. It is built on the
|
|
360
|
+
* `Resizable` primitive, so each line between two columns is a `role="separator"`.
|
|
361
|
+
*
|
|
362
|
+
* <SplitView as |view|>
|
|
363
|
+
* <view.Sidebar>…</view.Sidebar>
|
|
364
|
+
* <view.Content>…</view.Content>
|
|
365
|
+
* <view.Detail><view.SidebarToggle /> …</view.Detail>
|
|
366
|
+
* </SplitView>
|
|
367
|
+
*
|
|
368
|
+
* A view narrower than `@compactBreakpoint` shows one column at a time. It wraps the app, so it
|
|
369
|
+
* is a wrapper for the harnesses (`data-test-jn-wrapper`), and its elements are styled by
|
|
370
|
+
* `data-jn-split-view-*` attributes.
|
|
371
|
+
*/
|
|
372
|
+
export default class SplitView extends Component<SplitViewSignature> {
|
|
373
|
+
visibilityState = controllable(this, 'visibility', 'all')
|
|
374
|
+
columnState = controllable(this, 'compactColumn', 'sidebar')
|
|
375
|
+
|
|
376
|
+
/** The view's own width in CSS pixels. Infinity before the first measure. */
|
|
377
|
+
@tracked width = Infinity
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* @internal The resizable group that the tiled columns are panels of. A group scales its
|
|
381
|
+
* panels when one comes or goes. Thus each set of columns has a new group, which starts from
|
|
382
|
+
* the widths of the leading columns and gives the detail the rest.
|
|
383
|
+
*/
|
|
384
|
+
@cached
|
|
385
|
+
get group(): ResizableGroup {
|
|
386
|
+
void this.visibility
|
|
387
|
+
void this.isCompact
|
|
388
|
+
void this.sidebarLayout
|
|
389
|
+
return new ResizableGroup({
|
|
390
|
+
disabled: () => this.args.resizable === false,
|
|
391
|
+
disableCursor: () => this.args.resizable === false,
|
|
392
|
+
onLayoutChanged: () => this.layoutChanged,
|
|
393
|
+
})
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
#root: HTMLElement | undefined
|
|
397
|
+
|
|
398
|
+
/** The width each column needs, which the columns tell when they render. 0 is no column. */
|
|
399
|
+
#needs: Record<SplitViewColumn, number> = { sidebar: 0, content: 0, detail: 0 }
|
|
400
|
+
|
|
401
|
+
/** The widths the user gave the leading columns, for the cookie and for a column that returns. */
|
|
402
|
+
@tracked widths: { sidebar?: number; content?: number } = {}
|
|
403
|
+
|
|
404
|
+
/** The element that had focus when an overlaid sidebar opened, for focus to go back to. */
|
|
405
|
+
#opener: HTMLElement | undefined
|
|
406
|
+
|
|
407
|
+
/** Whether the overlaid sidebar takes focus when it renders: only when the user opened it. */
|
|
408
|
+
#focusSidebar = false
|
|
409
|
+
|
|
410
|
+
/** Whether focus was in the view when a compact view changed its column. */
|
|
411
|
+
#moveFocus = false
|
|
412
|
+
|
|
413
|
+
get visibility(): SplitViewVisibility {
|
|
414
|
+
return this.visibilityState.value
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
get compactColumn(): SplitViewColumn {
|
|
418
|
+
return this.columnState.value
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
get isCompact(): boolean {
|
|
422
|
+
return this.width < (this.args.compactBreakpoint ?? SPLIT_VIEW_COMPACT_BREAKPOINT)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
get isSidebarShown(): boolean {
|
|
426
|
+
return this.visibility === 'all'
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
get canGoBack(): boolean {
|
|
430
|
+
return this.isCompact && this.compactColumn !== 'sidebar'
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* How a wide view shows its sidebar. It is `overlaid` or `displaced` only while the view is
|
|
435
|
+
* narrower than the widths its columns need. The needs are not tracked: they are known after
|
|
436
|
+
* the first render, and the width is not known before that.
|
|
437
|
+
*/
|
|
438
|
+
get sidebarLayout(): 'tiled' | 'overlaid' | 'displaced' | 'hidden' | undefined {
|
|
439
|
+
if (this.isCompact) return undefined
|
|
440
|
+
if (!this.isSidebarShown) return 'hidden'
|
|
441
|
+
const needs = this.#needs
|
|
442
|
+
const mode = this.args.mode ?? 'tile'
|
|
443
|
+
if (mode === 'tile' || this.width >= needs.sidebar + needs.content + needs.detail) {
|
|
444
|
+
return 'tiled'
|
|
445
|
+
}
|
|
446
|
+
return mode === 'overlay' ? 'overlaid' : 'displaced'
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** @internal A column tells the width it needs. */
|
|
450
|
+
note = (column: SplitViewColumn, width: number): undefined => {
|
|
451
|
+
this.#needs[column] = width
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** @internal Whether a leading column renders. A compact view renders each, and shows one. */
|
|
455
|
+
shows = (column: 'sidebar' | 'content'): boolean =>
|
|
456
|
+
this.isCompact ||
|
|
457
|
+
(column === 'sidebar' ? this.isSidebarShown : this.visibility !== 'detail-only')
|
|
458
|
+
|
|
459
|
+
/** @internal Whether a leading column is over the detail, not a panel of the group. */
|
|
460
|
+
floats = (column: 'sidebar' | 'content'): boolean =>
|
|
461
|
+
column === 'sidebar' && this.sidebarLayout === 'overlaid'
|
|
462
|
+
|
|
463
|
+
/** @internal */
|
|
464
|
+
columnId = (column: SplitViewColumn): string => idFor(this, column)
|
|
465
|
+
|
|
466
|
+
/** @internal A column's first width: the one the user gave it, or its default. */
|
|
467
|
+
widthOf = (column: 'sidebar' | 'content', fallback: number): number =>
|
|
468
|
+
this.widths[column] ?? fallback
|
|
469
|
+
|
|
470
|
+
setVisibility = (visibility: SplitViewVisibility): void => {
|
|
471
|
+
const hides = this.isSidebarShown && visibility !== 'all'
|
|
472
|
+
const active = this.#root?.ownerDocument.activeElement
|
|
473
|
+
if (hides) this.#keepFocus(active)
|
|
474
|
+
this.visibilityState.set(visibility)
|
|
475
|
+
if (!hides && this.sidebarLayout === 'overlaid' && !this.#focusSidebar) {
|
|
476
|
+
this.#opener = active instanceof HTMLElement ? active : undefined
|
|
477
|
+
this.#focusSidebar = true
|
|
478
|
+
}
|
|
479
|
+
this.#save()
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* The sidebar goes from the page when it hides, so focus in it would be lost. Focus goes to
|
|
484
|
+
* the element that opened an overlaid sidebar, or to a toggle outside the sidebar.
|
|
485
|
+
*/
|
|
486
|
+
#keepFocus(active: Element | null | undefined): void {
|
|
487
|
+
const sidebar = this.#column('sidebar')
|
|
488
|
+
const opener = this.#opener
|
|
489
|
+
this.#opener = undefined
|
|
490
|
+
if (!sidebar || !active || !(sidebar === active || sidebar.contains(active))) return
|
|
491
|
+
const target = opener?.isConnected
|
|
492
|
+
? opener
|
|
493
|
+
: [...this.#root!.querySelectorAll<HTMLElement>('[data-jn-split-view-toggle]')].find(
|
|
494
|
+
toggle => !sidebar.contains(toggle),
|
|
495
|
+
)
|
|
496
|
+
target?.focus()
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
toggleSidebar = (): void => {
|
|
500
|
+
if (this.isCompact) this.showColumn('sidebar')
|
|
501
|
+
else if (!this.isSidebarShown) this.setVisibility('all')
|
|
502
|
+
else this.setVisibility(this.#needs.content ? 'double-column' : 'detail-only')
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
showColumn = (column: SplitViewColumn): void => {
|
|
506
|
+
const root = this.#root
|
|
507
|
+
this.#moveFocus =
|
|
508
|
+
this.isCompact &&
|
|
509
|
+
column !== this.compactColumn &&
|
|
510
|
+
!!root?.contains(root.ownerDocument.activeElement)
|
|
511
|
+
this.columnState.set(column)
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* A compact view hides the column that had focus, and a back button can go. Focus then goes
|
|
516
|
+
* to the column that shows, so it is not lost to the body.
|
|
517
|
+
*/
|
|
518
|
+
#focusColumn(column: SplitViewColumn): void {
|
|
519
|
+
if (!this.#moveFocus) return
|
|
520
|
+
this.#moveFocus = false
|
|
521
|
+
const active = this.#root?.ownerDocument.activeElement
|
|
522
|
+
// Focus is still on an element that shows: the user keeps it.
|
|
523
|
+
if (active && active !== active.ownerDocument.body && active.getClientRects().length) return
|
|
524
|
+
const target = this.#column(column)
|
|
525
|
+
if (!target) return
|
|
526
|
+
if (!target.hasAttribute('tabindex')) target.setAttribute('tabindex', '-1')
|
|
527
|
+
target.focus({ preventScroll: true })
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
back = (): void => {
|
|
531
|
+
const column = this.compactColumn
|
|
532
|
+
if (column === 'detail') this.showColumn(this.#needs.content ? 'content' : 'sidebar')
|
|
533
|
+
else if (column === 'content') this.showColumn('sidebar')
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
#column(column: SplitViewColumn): HTMLElement | null {
|
|
537
|
+
return (
|
|
538
|
+
this.#root?.querySelector<HTMLElement>(
|
|
539
|
+
`:scope > [data-jn-split-view-group] > [data-jn-split-view-column="${column}"]`,
|
|
540
|
+
) ?? null
|
|
541
|
+
)
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** The user moved a separator: keeps the widths of the leading columns. */
|
|
545
|
+
layoutChanged = (_: number[], __: ResizableLayoutMap, meta: ResizableLayoutMeta): void => {
|
|
546
|
+
if (!meta.isUserInteraction || this.isCompact) return
|
|
547
|
+
const widths = { ...this.widths }
|
|
548
|
+
for (const column of ['sidebar', 'content'] as const) {
|
|
549
|
+
const width = this.#column(column)?.offsetWidth
|
|
550
|
+
if (width && !this.floats(column)) widths[column] = width
|
|
551
|
+
}
|
|
552
|
+
this.widths = widths
|
|
553
|
+
this.#save()
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
#save(): void {
|
|
557
|
+
const name = this.args.cookieName ?? SPLIT_VIEW_COOKIE_NAME
|
|
558
|
+
const doc = this.#root?.ownerDocument
|
|
559
|
+
if (name === false || !doc) return
|
|
560
|
+
const state: SplitViewState = { visibility: this.visibility, ...this.widths }
|
|
561
|
+
const value = encodeURIComponent(JSON.stringify(state))
|
|
562
|
+
doc.cookie = `${name}=${value}; path=/; max-age=${SPLIT_VIEW_COOKIE_MAX_AGE}`
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** @internal Measures the root (not the window), and hears Escape and the harness. */
|
|
566
|
+
setup: PartModifier = part(() => {
|
|
567
|
+
// The part runs again after each render that changes the column, when the column shows.
|
|
568
|
+
this.#focusColumn(this.compactColumn)
|
|
569
|
+
return this.#setup
|
|
570
|
+
})
|
|
571
|
+
|
|
572
|
+
#setup: PartSpec = {
|
|
573
|
+
element: element => {
|
|
574
|
+
this.#root = element
|
|
575
|
+
const measure = () => {
|
|
576
|
+
if (this.isDestroying) return
|
|
577
|
+
// The width the group is wider by while the sidebar displaces the detail.
|
|
578
|
+
element.style.setProperty('--jn-split-view-sidebar', `${this.#needs.sidebar}px`)
|
|
579
|
+
if (element.offsetWidth !== this.width) this.width = element.offsetWidth
|
|
580
|
+
}
|
|
581
|
+
// Not during this render, which has read the width already.
|
|
582
|
+
queueMicrotask(measure)
|
|
583
|
+
const observer = new ResizeObserver(measure)
|
|
584
|
+
observer.observe(element)
|
|
585
|
+
const show = (event: Event) => this.showColumn((event as CustomEvent<SplitViewColumn>).detail)
|
|
586
|
+
// For `SplitViewHarness.showColumn()`, which has no control of the app's to click.
|
|
587
|
+
element.addEventListener('joinery:show-column', show)
|
|
588
|
+
return () => {
|
|
589
|
+
observer.disconnect()
|
|
590
|
+
element.removeEventListener('joinery:show-column', show)
|
|
591
|
+
}
|
|
592
|
+
},
|
|
593
|
+
on: {
|
|
594
|
+
keydown: event => {
|
|
595
|
+
if (event.key !== 'Escape' || this.sidebarLayout !== 'overlaid') return
|
|
596
|
+
event.preventDefault()
|
|
597
|
+
this.toggleSidebar()
|
|
598
|
+
},
|
|
599
|
+
},
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** @internal The overlaid sidebar takes focus when the user opened it. */
|
|
603
|
+
floating: PartModifier = part(() => ({
|
|
604
|
+
element: element => {
|
|
605
|
+
if (this.#focusSidebar) element.focus({ preventScroll: true })
|
|
606
|
+
this.#focusSidebar = false
|
|
607
|
+
},
|
|
608
|
+
}))
|
|
609
|
+
|
|
610
|
+
<template>
|
|
611
|
+
<div
|
|
612
|
+
data-jn-split-view
|
|
613
|
+
data-visibility={{this.visibility}}
|
|
614
|
+
data-mode={{if @mode @mode "tile"}}
|
|
615
|
+
data-sidebar={{this.sidebarLayout}}
|
|
616
|
+
data-compact={{if this.isCompact ""}}
|
|
617
|
+
data-compact-column={{this.compactColumn}}
|
|
618
|
+
data-test-jn="split-view"
|
|
619
|
+
data-test-jn-wrapper
|
|
620
|
+
data-test-jn-name={{@name}}
|
|
621
|
+
...attributes
|
|
622
|
+
{{this.setup}}
|
|
623
|
+
>
|
|
624
|
+
<div data-jn-split-view-group {{this.group.group disabled=(isFalse @resizable)}}>
|
|
625
|
+
{{yield
|
|
626
|
+
(hash
|
|
627
|
+
Sidebar=(component Leading view=this column="sidebar")
|
|
628
|
+
Content=(component Leading view=this column="content")
|
|
629
|
+
Detail=(component Detail view=this)
|
|
630
|
+
SidebarToggle=(component SidebarToggle view=this)
|
|
631
|
+
BackButton=(component BackButton view=this)
|
|
632
|
+
visibility=this.visibility
|
|
633
|
+
setVisibility=this.setVisibility
|
|
634
|
+
toggleSidebar=this.toggleSidebar
|
|
635
|
+
isCompact=this.isCompact
|
|
636
|
+
compactColumn=this.compactColumn
|
|
637
|
+
showColumn=this.showColumn
|
|
638
|
+
back=this.back
|
|
639
|
+
)
|
|
640
|
+
}}
|
|
641
|
+
</div>
|
|
642
|
+
{{#if (isOverlaid this.sidebarLayout)}}
|
|
643
|
+
{{! A click away closes the sidebar. The keyboard has Escape and the toggle. }}
|
|
644
|
+
<div data-jn-split-view-backdrop aria-hidden="true" {{on "click" this.toggleSidebar}}></div>
|
|
645
|
+
{{/if}}
|
|
646
|
+
</div>
|
|
647
|
+
</template>
|
|
648
|
+
}
|