@podoba/react 0.0.51 → 0.0.53
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/package.json +4 -4
- package/src/components/asset-selection-surface.tsx +264 -67
- package/src/components/citation.examples.tsx +14 -0
- package/src/components/citation.tsx +27 -0
- package/src/components/column-layout.examples.tsx +34 -0
- package/src/components/column-layout.tsx +36 -0
- package/src/components/combobox.tsx +9 -4
- package/src/components/compact-settings-dialog.tsx +80 -21
- package/src/components/date-picker.tsx +49 -8
- package/src/components/delivery/send-to-print-modal.tsx +191 -225
- package/src/components/field-appearance.ts +11 -1
- package/src/components/icon-button.examples.tsx +16 -0
- package/src/components/icon-button.tsx +20 -0
- package/src/components/inline-alert.examples.tsx +8 -0
- package/src/components/inline-alert.tsx +23 -0
- package/src/components/input.tsx +25 -12
- package/src/components/island-toolbar.examples.tsx +27 -0
- package/src/components/island-toolbar.tsx +39 -0
- package/src/components/media-asset-panel.examples.tsx +44 -5
- package/src/components/media-asset-panel.tsx +339 -28
- package/src/components/media-collection.examples.tsx +18 -0
- package/src/components/media-collection.tsx +206 -0
- package/src/components/number-field.tsx +18 -9
- package/src/components/preview-error.examples.tsx +20 -0
- package/src/components/preview-error.tsx +29 -0
- package/src/components/preview-metadata-card.examples.tsx +32 -0
- package/src/components/preview-metadata-card.tsx +112 -0
- package/src/components/search-field.tsx +27 -7
- package/src/components/select.tsx +81 -50
- package/src/components/settings-dialog-surface.tsx +553 -114
- package/src/components/template-catalog-card.tsx +309 -78
- package/src/components/textarea.tsx +160 -12
- package/src/components/tile.tsx +30 -16
- package/src/components/zoomable-image.examples.tsx +33 -0
- package/src/components/zoomable-image.tsx +185 -0
- package/src/index.ts +98 -78
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
import { Button } from './button'
|
|
3
|
+
import { uic } from '../utils/uic'
|
|
4
|
+
|
|
5
|
+
/** Shared Studio/editor island. The app owns commands and selection retention. */
|
|
6
|
+
export const IslandToolbar = uic('div', {
|
|
7
|
+
displayName: 'IslandToolbar',
|
|
8
|
+
baseClass:
|
|
9
|
+
'flex max-w-full items-center gap-1 overflow-x-auto rounded-full bg-surface-inverted px-2 py-1 text-fg-inverted shadow-lg',
|
|
10
|
+
defaultProps: { role: 'toolbar' },
|
|
11
|
+
})
|
|
12
|
+
export const IslandToolbarSeparator = uic('span', {
|
|
13
|
+
displayName: 'IslandToolbarSeparator',
|
|
14
|
+
baseClass: 'mx-1 h-5 w-px shrink-0 bg-white/25',
|
|
15
|
+
defaultProps: { 'aria-hidden': true },
|
|
16
|
+
})
|
|
17
|
+
const Control = uic(Button, {
|
|
18
|
+
displayName: 'IslandToolbarControl',
|
|
19
|
+
baseClass:
|
|
20
|
+
'group/island-control h-8 min-w-8 shrink-0 justify-center gap-0 rounded-full px-2 py-0 text-fg-inverted hover:gap-2 hover:bg-white/15 focus-visible:gap-2 aria-pressed:bg-white/20',
|
|
21
|
+
defaultVariants: { variant: 'ghost', size: 'sm' },
|
|
22
|
+
})
|
|
23
|
+
export function IslandToolbarButton({
|
|
24
|
+
label,
|
|
25
|
+
icon,
|
|
26
|
+
...props
|
|
27
|
+
}: Omit<Parameters<typeof Control>[0], 'children'> & { label: string; icon: ReactNode }) {
|
|
28
|
+
return (
|
|
29
|
+
<Control {...props} aria-label={label}>
|
|
30
|
+
{icon}
|
|
31
|
+
<span
|
|
32
|
+
aria-hidden="true"
|
|
33
|
+
className="max-w-0 overflow-hidden truncate whitespace-nowrap text-compact font-normal leading-4 opacity-0 transition-[max-width,opacity] duration-150 group-hover/island-control:max-w-40 group-hover/island-control:opacity-100 group-focus-visible/island-control:max-w-40 group-focus-visible/island-control:opacity-100 motion-reduce:transition-none"
|
|
34
|
+
>
|
|
35
|
+
{label}
|
|
36
|
+
</span>
|
|
37
|
+
</Control>
|
|
38
|
+
)
|
|
39
|
+
}
|
|
@@ -1,11 +1,50 @@
|
|
|
1
1
|
import { MediaAssetPanel } from './media-asset-panel'
|
|
2
|
-
const labels = {
|
|
3
|
-
|
|
2
|
+
const labels = {
|
|
3
|
+
heading: 'Assets',
|
|
4
|
+
emptyDescription: 'No assets yet.',
|
|
5
|
+
emptyHint: 'Select assets to begin.',
|
|
6
|
+
selectLabel: 'Select assets',
|
|
7
|
+
previousLabel: 'Previous asset',
|
|
8
|
+
nextLabel: 'Next asset',
|
|
9
|
+
}
|
|
10
|
+
const items = ['First', 'Second'].map((id) => ({
|
|
11
|
+
id,
|
|
12
|
+
content: <div className="grid h-full place-items-center bg-surface-muted">{id}</div>,
|
|
13
|
+
}))
|
|
4
14
|
export const examples = {
|
|
15
|
+
readability: () => (
|
|
16
|
+
<MediaAssetPanel
|
|
17
|
+
{...labels}
|
|
18
|
+
items={items}
|
|
19
|
+
mode="media"
|
|
20
|
+
empty={false}
|
|
21
|
+
identity={{
|
|
22
|
+
title: 'Title',
|
|
23
|
+
caption: 'Independent caption',
|
|
24
|
+
count: 1,
|
|
25
|
+
titleAppearance: { color: 'dark', background: 'none' },
|
|
26
|
+
captionAppearance: { color: 'white', background: 'blur' },
|
|
27
|
+
}}
|
|
28
|
+
/>
|
|
29
|
+
),
|
|
5
30
|
default: () => <MediaAssetPanel {...labels} items={items} mode="media" empty={false} onSelect={() => undefined} />,
|
|
6
|
-
variants: () =>
|
|
31
|
+
variants: () => (
|
|
32
|
+
<MediaAssetPanel {...labels} items={items} mode="carousel" empty={false} onSelect={() => undefined} />
|
|
33
|
+
),
|
|
7
34
|
states: () => <MediaAssetPanel {...labels} items={[]} mode="media" empty onSelect={() => undefined} />,
|
|
8
35
|
readOnly: () => <MediaAssetPanel {...labels} items={items} mode="grid" empty={false} />,
|
|
9
|
-
reference: () =>
|
|
36
|
+
reference: () => (
|
|
37
|
+
<MediaAssetPanel
|
|
38
|
+
{...labels}
|
|
39
|
+
items={items.slice(0, 1)}
|
|
40
|
+
mode="media"
|
|
41
|
+
empty={false}
|
|
42
|
+
identity={{ title: 'Annual report', caption: '1 asset reference', count: 1 }}
|
|
43
|
+
onSelect={() => undefined}
|
|
44
|
+
/>
|
|
45
|
+
),
|
|
46
|
+
}
|
|
47
|
+
export const meta = {
|
|
48
|
+
category: 'Composite',
|
|
49
|
+
description: 'Asset preview panel with centered empty action and independent carousel controls.',
|
|
10
50
|
}
|
|
11
|
-
export const meta = { category: 'Composite', description: 'Asset preview panel with centered empty action and independent carousel controls.' }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { useEffect, useRef, useState, type CSSProperties, type ReactNode } from 'react'
|
|
2
2
|
import { Button as AriaButton } from 'react-aria-components'
|
|
3
3
|
import { uic } from '../utils/uic'
|
|
4
4
|
import { Button } from './button'
|
|
@@ -18,41 +18,352 @@ export type MediaAssetPanelProps = {
|
|
|
18
18
|
nextLabel: string
|
|
19
19
|
onSelect?: () => void
|
|
20
20
|
/** Optional reference identity; omitted for a media-only gallery. */
|
|
21
|
-
identity?: {
|
|
21
|
+
identity?: {
|
|
22
|
+
title: ReactNode
|
|
23
|
+
caption: ReactNode
|
|
24
|
+
count: ReactNode
|
|
25
|
+
titleAppearance?: MediaTextAppearance
|
|
26
|
+
captionAppearance?: MediaTextAppearance
|
|
27
|
+
captionWidth?: number
|
|
28
|
+
}
|
|
22
29
|
className?: string
|
|
23
30
|
}
|
|
24
31
|
const Surface = uic('div', {
|
|
25
32
|
displayName: 'MediaAssetPanel',
|
|
26
|
-
baseClass:
|
|
33
|
+
baseClass:
|
|
34
|
+
'group/media-panel relative isolate h-95 w-full shrink-0 overflow-hidden rounded-lg bg-surface-card text-fg',
|
|
27
35
|
})
|
|
28
36
|
const PanelAction = uic(AriaButton, {
|
|
29
37
|
displayName: 'MediaAssetPanelAction',
|
|
30
|
-
baseClass:
|
|
38
|
+
baseClass:
|
|
39
|
+
'absolute inset-0 z-10 cursor-pointer rounded-lg border-0 bg-transparent p-0 outline-none data-[focus-visible]:ring-2 data-[focus-visible]:ring-inset data-[focus-visible]:ring-ring',
|
|
31
40
|
})
|
|
32
41
|
|
|
33
42
|
/** Full-bleed media with independent selection and carousel controls. */
|
|
34
|
-
export function MediaAssetPanel({
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
43
|
+
export function MediaAssetPanel({
|
|
44
|
+
items,
|
|
45
|
+
mode,
|
|
46
|
+
empty,
|
|
47
|
+
heading,
|
|
48
|
+
emptyDescription,
|
|
49
|
+
emptyHint,
|
|
50
|
+
selectLabel,
|
|
51
|
+
previousLabel,
|
|
52
|
+
nextLabel,
|
|
53
|
+
onSelect,
|
|
54
|
+
identity,
|
|
55
|
+
className,
|
|
56
|
+
}: MediaAssetPanelProps) {
|
|
57
|
+
const panelRef = useRef<HTMLDivElement>(null)
|
|
58
|
+
const [multiply, setMultiply] = useState(false)
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const panel = panelRef.current
|
|
61
|
+
if (!panel) return
|
|
62
|
+
let disposed = false
|
|
63
|
+
const analyze = async () => {
|
|
64
|
+
const image = panel.querySelector<HTMLImageElement>('img')
|
|
65
|
+
if (!image?.complete || !image.naturalWidth) return
|
|
66
|
+
try {
|
|
67
|
+
const readable = await readableMediaImage(image)
|
|
68
|
+
const canvas = document.createElement('canvas')
|
|
69
|
+
canvas.width = 16
|
|
70
|
+
canvas.height = 16
|
|
71
|
+
const ctx = canvas.getContext('2d', { willReadFrequently: true })
|
|
72
|
+
if (!ctx) return
|
|
73
|
+
ctx.fillStyle = 'white'
|
|
74
|
+
ctx.fillRect(0, 0, 16, 16)
|
|
75
|
+
ctx.drawImage(readable, 0, 0, 16, 16)
|
|
76
|
+
const data = ctx.getImageData(0, 0, 16, 16).data
|
|
77
|
+
const white = (pixel: number) =>
|
|
78
|
+
(data[pixel * 4] ?? 0) >= 245 && (data[pixel * 4 + 1] ?? 0) >= 245 && (data[pixel * 4 + 2] ?? 0) >= 245
|
|
79
|
+
// A white backdrop surrounds the subject. Isolated highlights in a photo do not qualify.
|
|
80
|
+
const corners = [0, 15, 240, 255].filter(white).length
|
|
81
|
+
let whites = 0
|
|
82
|
+
for (let i = 0; i < 256; i++) if (white(i)) whites++
|
|
83
|
+
if (!disposed) setMultiply(corners >= 3 && whites / 256 >= 0.25)
|
|
84
|
+
} catch {
|
|
85
|
+
if (!disposed) setMultiply(false)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
panel.addEventListener('load', analyze, true)
|
|
89
|
+
const observer = new MutationObserver(analyze)
|
|
90
|
+
observer.observe(panel, { childList: true, subtree: true, attributes: true, attributeFilter: ['src'] })
|
|
91
|
+
void analyze()
|
|
92
|
+
return () => {
|
|
93
|
+
disposed = true
|
|
94
|
+
panel.removeEventListener('load', analyze, true)
|
|
95
|
+
observer.disconnect()
|
|
96
|
+
}
|
|
97
|
+
}, [empty, mode, items.map((item) => item.id).join(',')])
|
|
98
|
+
return (
|
|
99
|
+
<Surface ref={panelRef} className={className} data-media-readability data-media-multiply={multiply}>
|
|
100
|
+
{onSelect ? (
|
|
101
|
+
empty ? (
|
|
102
|
+
<button
|
|
103
|
+
type="button"
|
|
104
|
+
aria-label={selectLabel}
|
|
105
|
+
aria-hidden="true"
|
|
106
|
+
tabIndex={-1}
|
|
107
|
+
onClick={onSelect}
|
|
108
|
+
className="absolute inset-0 z-10 cursor-pointer rounded-lg border-0 bg-transparent p-0"
|
|
109
|
+
/>
|
|
110
|
+
) : (
|
|
111
|
+
<PanelAction aria-label={selectLabel} onPress={onSelect} />
|
|
112
|
+
)
|
|
113
|
+
) : null}
|
|
114
|
+
{empty ? (
|
|
115
|
+
<>
|
|
116
|
+
<div className="pointer-events-none absolute inset-x-4 top-4 z-20 text-body font-medium leading-5">
|
|
117
|
+
{heading}
|
|
118
|
+
</div>
|
|
119
|
+
{onSelect ? (
|
|
120
|
+
<div className="pointer-events-none absolute inset-0 z-20 flex items-center justify-center">
|
|
121
|
+
<Button className="pointer-events-auto h-14 w-48 p-0" onPress={onSelect}>
|
|
122
|
+
{selectLabel}
|
|
123
|
+
</Button>
|
|
124
|
+
</div>
|
|
125
|
+
) : null}
|
|
126
|
+
<p className="pointer-events-none absolute inset-x-4 bottom-4 z-20 m-0 text-small">
|
|
127
|
+
<span className="font-medium text-fg">{emptyDescription}</span>
|
|
128
|
+
<br />
|
|
129
|
+
<span className="text-fg-muted">{emptyHint}</span>
|
|
130
|
+
</p>
|
|
131
|
+
</>
|
|
132
|
+
) : (
|
|
133
|
+
<MediaGallery
|
|
134
|
+
items={mode === 'media' ? items.slice(0, 1) : items}
|
|
135
|
+
mode={mode === 'carousel' ? 'carousel' : 'grid'}
|
|
136
|
+
previousLabel={previousLabel}
|
|
137
|
+
nextLabel={nextLabel}
|
|
138
|
+
className={`pointer-events-none bg-transparent ${multiply ? 'mix-blend-multiply' : ''}`}
|
|
139
|
+
/>
|
|
140
|
+
)}
|
|
141
|
+
{!empty && identity ? (
|
|
142
|
+
<>
|
|
143
|
+
<div className="pointer-events-none absolute inset-4 z-[2] grid grid-rows-[auto_minmax(0,1fr)_auto] gap-6 text-fg">
|
|
144
|
+
<div className="flex items-start justify-between gap-2 text-body font-medium leading-5">
|
|
145
|
+
<div className="min-w-0 flex-1">
|
|
146
|
+
{heading ? <span className="mb-1 block text-caption">{heading}</span> : null}
|
|
147
|
+
<p className="m-0 text-body font-medium leading-5">
|
|
148
|
+
<MediaReadableText appearance={identity.titleAppearance}>{identity.title}</MediaReadableText>
|
|
149
|
+
</p>
|
|
150
|
+
</div>
|
|
151
|
+
<span className="shrink-0 rounded-full bg-surface-muted px-2.5 py-1 text-caption text-fg [text-shadow:none]">
|
|
152
|
+
{identity.count}
|
|
153
|
+
</span>
|
|
154
|
+
</div>
|
|
155
|
+
<span aria-hidden="true" />
|
|
156
|
+
<p className="m-0 text-small">
|
|
157
|
+
<MediaReadableText appearance={identity.captionAppearance} secondary maxWidth={identity.captionWidth}>
|
|
158
|
+
{identity.caption}
|
|
159
|
+
</MediaReadableText>
|
|
160
|
+
</p>
|
|
161
|
+
</div>
|
|
162
|
+
</>
|
|
163
|
+
) : null}
|
|
164
|
+
</Surface>
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export const MEDIA_TEXT_COLORS = ['auto', 'dark', 'white', 'gray'] as const
|
|
169
|
+
export const MEDIA_TEXT_BACKGROUNDS = ['auto', 'none', 'gradient', 'blur'] as const
|
|
170
|
+
export type MediaTextColor = (typeof MEDIA_TEXT_COLORS)[number]
|
|
171
|
+
export type MediaTextBackground = (typeof MEDIA_TEXT_BACKGROUNDS)[number]
|
|
172
|
+
export type MediaTextAppearance = { color?: MediaTextColor; background?: MediaTextBackground }
|
|
173
|
+
type Tone = Exclude<MediaTextColor, 'auto'>
|
|
174
|
+
export type MediaPixel = readonly [number, number, number]
|
|
175
|
+
const inks: Record<Tone, MediaPixel> = { dark: [23, 23, 23], white: [255, 255, 255], gray: [82, 82, 82] }
|
|
176
|
+
const luminance = (rgb: MediaPixel) =>
|
|
177
|
+
rgb
|
|
178
|
+
.map((v) => v / 255)
|
|
179
|
+
.map((v) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4))
|
|
180
|
+
.reduce((sum, v, i) => sum + v * ([0.2126, 0.7152, 0.0722][i] ?? 0), 0)
|
|
181
|
+
export function mediaContrast(a: MediaPixel, b: MediaPixel): number {
|
|
182
|
+
const x = luminance(a),
|
|
183
|
+
y = luminance(b)
|
|
184
|
+
return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05)
|
|
185
|
+
}
|
|
186
|
+
/** Conservative local contrast, including the final tint. Blur alone never counts as contrast protection. */
|
|
187
|
+
export function resolveMediaText(
|
|
188
|
+
pixels: readonly MediaPixel[],
|
|
189
|
+
appearance: MediaTextAppearance = {},
|
|
190
|
+
_secondary = false,
|
|
191
|
+
) {
|
|
192
|
+
const samples = pixels.length ? pixels : [[0, 0, 0] as const, [255, 255, 255] as const]
|
|
193
|
+
const lightness = samples.map(luminance).sort((a, b) => a - b)
|
|
194
|
+
const median = lightness[Math.floor(lightness.length / 2)] ?? 0.5
|
|
195
|
+
const color: Tone =
|
|
196
|
+
appearance.color && appearance.color !== 'auto' ? appearance.color : median >= 0.35 ? 'dark' : 'white'
|
|
197
|
+
const score = Math.min(...samples.map((pixel) => mediaContrast(inks[color], pixel)))
|
|
198
|
+
const range =
|
|
199
|
+
(lightness[Math.floor(lightness.length * 0.9)] ?? median) -
|
|
200
|
+
(lightness[Math.floor(lightness.length * 0.1)] ?? median)
|
|
201
|
+
const busy = range > 0.25
|
|
202
|
+
const requested = appearance.background && appearance.background !== 'auto' ? appearance.background : undefined
|
|
203
|
+
const background = requested ?? (score >= 4.5 && !busy ? 'none' : 'gradient')
|
|
204
|
+
const tint = color === 'white' ? 0 : 255
|
|
205
|
+
const target = busy ? 7 : 4.5
|
|
206
|
+
let opacity = 0
|
|
207
|
+
if (background !== 'none') {
|
|
208
|
+
while (
|
|
209
|
+
opacity < 1 &&
|
|
210
|
+
samples.some(
|
|
211
|
+
(pixel) =>
|
|
212
|
+
mediaContrast(
|
|
213
|
+
inks[color],
|
|
214
|
+
pixel.map((value) => value * (1 - opacity) + tint * opacity) as [number, number, number],
|
|
215
|
+
) < target,
|
|
216
|
+
)
|
|
217
|
+
)
|
|
218
|
+
opacity = Math.min(1, opacity + 0.01)
|
|
219
|
+
if (busy && color !== 'white') opacity = Math.max(0.8, opacity)
|
|
220
|
+
}
|
|
221
|
+
return { color, background, opacity, tint }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const toneClass: Record<Tone, string> = { dark: 'text-neutral-900', white: 'text-white', gray: 'text-neutral-600' }
|
|
225
|
+
|
|
226
|
+
/** Used inside the existing media panel. Each text region measures its own visible image crop. */
|
|
227
|
+
export function MediaReadableText({
|
|
228
|
+
children,
|
|
229
|
+
appearance,
|
|
230
|
+
secondary = false,
|
|
231
|
+
maxWidth,
|
|
232
|
+
}: {
|
|
233
|
+
children: ReactNode
|
|
234
|
+
appearance?: MediaTextAppearance
|
|
235
|
+
secondary?: boolean
|
|
236
|
+
maxWidth?: number
|
|
237
|
+
}) {
|
|
238
|
+
const ref = useRef<HTMLSpanElement>(null)
|
|
239
|
+
const [pixels, setPixels] = useState<MediaPixel[]>([])
|
|
240
|
+
const [panelBox, setPanelBox] = useState<{ left: number; top: number; width: number; height: number } | null>(null)
|
|
241
|
+
const [analysisError, setAnalysisError] = useState<string | null>(null)
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
const text = ref.current,
|
|
244
|
+
panel = text?.closest<HTMLElement>('[data-media-readability]')
|
|
245
|
+
if (!text || !panel) return
|
|
246
|
+
let frame = 0
|
|
247
|
+
const measure = () => {
|
|
248
|
+
cancelAnimationFrame(frame)
|
|
249
|
+
frame = requestAnimationFrame(async () => {
|
|
250
|
+
const image = panel.querySelector<HTMLImageElement>('img')
|
|
251
|
+
if (!image?.complete || !image.naturalWidth) {
|
|
252
|
+
setPixels([])
|
|
253
|
+
return
|
|
254
|
+
}
|
|
255
|
+
const box = text.getBoundingClientRect(),
|
|
256
|
+
rect = image.getBoundingClientRect()
|
|
257
|
+
const bounds = panel.getBoundingClientRect()
|
|
258
|
+
setPanelBox({
|
|
259
|
+
left: bounds.left - box.left,
|
|
260
|
+
top: bounds.top - box.top,
|
|
261
|
+
width: bounds.width,
|
|
262
|
+
height: bounds.height,
|
|
263
|
+
})
|
|
264
|
+
if (!box.width || !box.height || !rect.width || !rect.height) return
|
|
265
|
+
const imageStyle = getComputedStyle(image)
|
|
266
|
+
const scale =
|
|
267
|
+
imageStyle.objectFit === 'contain'
|
|
268
|
+
? Math.min(rect.width / image.naturalWidth, rect.height / image.naturalHeight)
|
|
269
|
+
: Math.max(rect.width / image.naturalWidth, rect.height / image.naturalHeight)
|
|
270
|
+
const position = imageStyle.objectPosition
|
|
271
|
+
.split(' ')
|
|
272
|
+
.map((part) => (part.endsWith('%') ? parseFloat(part) / 100 : 0.5))
|
|
273
|
+
const sx = (image.naturalWidth - rect.width / scale) * (position[0] ?? 0.5) + (box.left - rect.left) / scale
|
|
274
|
+
const sy = (image.naturalHeight - rect.height / scale) * (position[1] ?? 0.5) + (box.top - rect.top) / scale
|
|
275
|
+
const canvas = document.createElement('canvas')
|
|
276
|
+
canvas.width = 32
|
|
277
|
+
canvas.height = 16
|
|
278
|
+
const ctx = canvas.getContext('2d', { willReadFrequently: true })
|
|
279
|
+
if (!ctx) return
|
|
280
|
+
try {
|
|
281
|
+
ctx.fillStyle = getComputedStyle(panel).backgroundColor
|
|
282
|
+
ctx.fillRect(0, 0, 32, 16)
|
|
283
|
+
ctx.globalCompositeOperation = image.closest('.mix-blend-multiply') ? 'multiply' : 'source-over'
|
|
284
|
+
const readable = await readableMediaImage(image)
|
|
285
|
+
if (!text.isConnected) return
|
|
286
|
+
ctx.drawImage(readable, sx, sy, box.width / scale, box.height / scale, 0, 0, 32, 16)
|
|
287
|
+
const data = ctx.getImageData(0, 0, 32, 16).data,
|
|
288
|
+
next: MediaPixel[] = []
|
|
289
|
+
for (let i = 0; i < data.length; i += 4) next.push([data[i] ?? 255, data[i + 1] ?? 255, data[i + 2] ?? 255])
|
|
290
|
+
setAnalysisError(null)
|
|
291
|
+
setPixels(next)
|
|
292
|
+
} catch (error) {
|
|
293
|
+
setAnalysisError(error instanceof Error ? error.message : 'Unavailable')
|
|
294
|
+
setPixels([
|
|
295
|
+
[0, 0, 0],
|
|
296
|
+
[255, 255, 255],
|
|
297
|
+
])
|
|
298
|
+
}
|
|
299
|
+
})
|
|
300
|
+
}
|
|
301
|
+
const resize = new ResizeObserver(measure)
|
|
302
|
+
resize.observe(panel)
|
|
303
|
+
resize.observe(text)
|
|
304
|
+
const mutation = new MutationObserver(measure)
|
|
305
|
+
mutation.observe(panel, { childList: true, subtree: true, attributes: true, attributeFilter: ['src', 'class'] })
|
|
306
|
+
panel.addEventListener('load', measure, true)
|
|
307
|
+
panel.addEventListener('error', measure, true)
|
|
308
|
+
measure()
|
|
309
|
+
return () => {
|
|
310
|
+
cancelAnimationFrame(frame)
|
|
311
|
+
resize.disconnect()
|
|
312
|
+
mutation.disconnect()
|
|
313
|
+
panel.removeEventListener('load', measure, true)
|
|
314
|
+
panel.removeEventListener('error', measure, true)
|
|
315
|
+
}
|
|
316
|
+
}, [children])
|
|
317
|
+
const resolved = resolveMediaText(pixels, appearance, secondary)
|
|
318
|
+
const tint = `rgba(${resolved.tint},${resolved.tint},${resolved.tint},${resolved.opacity})`
|
|
319
|
+
const effectStyle: CSSProperties = panelBox
|
|
320
|
+
? {
|
|
321
|
+
left: panelBox.left,
|
|
322
|
+
top: panelBox.top,
|
|
323
|
+
width: panelBox.width,
|
|
324
|
+
height: panelBox.height,
|
|
325
|
+
background: tint,
|
|
326
|
+
maskImage: `radial-gradient(ellipse 110% 95% at left ${secondary ? 'bottom' : 'top'},black 0%,black 40%,rgba(0,0,0,.8) 55%,rgba(0,0,0,.35) 75%,transparent 100%)`,
|
|
327
|
+
}
|
|
328
|
+
: {
|
|
329
|
+
inset: 'calc(var(--spacing) * -12)',
|
|
330
|
+
background: `radial-gradient(ellipse at center,${tint} 0%,${tint} 35%,transparent 75%)`,
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return (
|
|
334
|
+
<span
|
|
335
|
+
ref={ref}
|
|
336
|
+
className={`relative isolate block w-fit max-w-full ${toneClass[resolved.color]}`}
|
|
337
|
+
style={{ maxWidth: maxWidth ? `${maxWidth * 100}%` : undefined, textShadow: 'none' }}
|
|
338
|
+
data-media-analysis-error={analysisError ?? undefined}
|
|
339
|
+
data-media-text-color={resolved.color}
|
|
340
|
+
data-media-text-background={resolved.background}
|
|
341
|
+
>
|
|
342
|
+
{resolved.background !== 'none' ? (
|
|
343
|
+
<span
|
|
344
|
+
aria-hidden="true"
|
|
345
|
+
className={`pointer-events-none absolute -z-10 ${panelBox ? '' : ''} ${resolved.background === 'blur' ? 'backdrop-blur-sm' : ''}`}
|
|
346
|
+
style={effectStyle}
|
|
347
|
+
/>
|
|
348
|
+
) : null}
|
|
349
|
+
{children}
|
|
350
|
+
</span>
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// A CORS-enabled copy keeps image analysis separate from the visible preview.
|
|
355
|
+
const readableMediaImages = new WeakMap<HTMLImageElement, { src: string; image: Promise<HTMLImageElement> }>()
|
|
356
|
+
function readableMediaImage(source: HTMLImageElement): Promise<HTMLImageElement> {
|
|
357
|
+
const src = source.currentSrc || source.src
|
|
358
|
+
const cached = readableMediaImages.get(source)
|
|
359
|
+
if (cached?.src === src) return cached.image
|
|
360
|
+
const image = new Promise<HTMLImageElement>((resolve, reject) => {
|
|
361
|
+
const copy = new Image()
|
|
362
|
+
copy.crossOrigin = 'anonymous'
|
|
363
|
+
copy.onload = () => resolve(copy)
|
|
364
|
+
copy.onerror = () => reject(new Error('Media analysis unavailable'))
|
|
365
|
+
copy.src = src
|
|
366
|
+
})
|
|
367
|
+
readableMediaImages.set(source, { src, image })
|
|
368
|
+
return image
|
|
58
369
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { MediaCarousel, MediaPanelCard, MediaPreviewScene } from './media-collection'
|
|
2
|
+
const items = [
|
|
3
|
+
{ id: 'a', title: 'Artwork', preview: <div className="h-full w-full bg-brand-green" /> },
|
|
4
|
+
{ id: 'b', title: 'Second artwork', preview: <div className="h-full w-full bg-accent-yellow" /> },
|
|
5
|
+
]
|
|
6
|
+
export const examples = {
|
|
7
|
+
default: () => <MediaCarousel items={items} label="Artwork" previousLabel="Previous" nextLabel="Next" />,
|
|
8
|
+
variants: () => <MediaPanelCard title="Artwork" caption="Caption" preview={items[0]!.preview} onOpen={() => {}} />,
|
|
9
|
+
states: () => (
|
|
10
|
+
<MediaPreviewScene>
|
|
11
|
+
<MediaCarousel items={items.slice(0, 1)} label="Single artwork" previousLabel="Previous" nextLabel="Next" />
|
|
12
|
+
</MediaPreviewScene>
|
|
13
|
+
),
|
|
14
|
+
}
|
|
15
|
+
export const meta = {
|
|
16
|
+
category: 'Composite',
|
|
17
|
+
description: 'Asset panels, contained carousel and lightweight media detail.',
|
|
18
|
+
}
|