(null)
+ useLayoutEffect(() => {
+ const el = ref.current
+ if (!el) return
+ let active = true
+ const run = () => {
+ if (active) fit(el)
+ }
+ run()
+ let observer: ResizeObserver | undefined
+ if (typeof ResizeObserver !== 'undefined') {
+ let lastWidth = el.clientWidth
+ // Fitting changes the element's own height, not its width; comparing widths avoids a feedback loop.
+ observer = new ResizeObserver(() => {
+ if (el.clientWidth === lastWidth) return
+ lastWidth = el.clientWidth
+ run()
+ })
+ observer.observe(el)
+ }
+ // The font loads late via @font-face and changes every measurement.
+ if (typeof document !== 'undefined' && document.fonts?.ready) void document.fonts.ready.then(run)
+ return () => {
+ active = false
+ observer?.disconnect()
+ }
+ }, [name])
+ return (
+
+ {name}
+
+ )
+}
diff --git a/web/src/features/modules/bring/Widget.test.tsx b/web/src/features/modules/bring/Widget.test.tsx
index 0e73b10..706909c 100644
--- a/web/src/features/modules/bring/Widget.test.tsx
+++ b/web/src/features/modules/bring/Widget.test.tsx
@@ -84,6 +84,70 @@ describe('bring widget', () => {
expect(container.querySelectorAll('.mirror-bring__fallback')).toHaveLength(1)
})
+ it('shows an emoji-only name once, as the symbol, without a name below', async () => {
+ setup(state([{ name: '🥛', iconUrl: '' }]))
+ const { container } = renderWithProviders('/')
+ await waitFor(() => expect(container.querySelector('.mirror-bring__emoji')).not.toBeNull())
+ expect(container.querySelectorAll('.mirror-bring__item')).toHaveLength(1)
+ expect(screen.getAllByText('🥛')).toHaveLength(1)
+ expect(container.querySelector('.mirror-bring__name')).toBeNull()
+ })
+
+ it('keeps the specification of an emoji-only name', async () => {
+ setup(state([{ name: '🥛', iconUrl: '', specification: '2 Liter' }]))
+ const { container } = renderWithProviders('/')
+ await screen.findByText('2 Liter')
+ expect(container.querySelector('.mirror-bring__name')).toBeNull()
+ })
+
+ it('shows the emoji as the symbol, without a name, when its picture fails to load', async () => {
+ setup(state([{ name: '🥛', iconUrl: 'https://example.test/milk.png' }]))
+ const { container } = renderWithProviders('/')
+ const img = await waitFor(() => {
+ const found = container.querySelector('img')
+ expect(found).not.toBeNull()
+ return found!
+ })
+ fireEvent.error(img)
+ await waitFor(() => expect(container.querySelector('.mirror-bring__emoji')).toHaveTextContent('🥛'))
+ expect(screen.getAllByText('🥛')).toHaveLength(1)
+ expect(container.querySelector('.mirror-bring__name')).toBeNull()
+ })
+
+ it('shows picture plus the emoji as name when the picture loads', async () => {
+ setup(state([{ name: '🥛', iconUrl: 'https://example.test/milk.png' }]))
+ const { container } = renderWithProviders('/')
+ await waitFor(() => expect(container.querySelector('img')).not.toBeNull())
+ expect(container.querySelector('.mirror-bring__name')).toHaveTextContent('🥛')
+ expect(container.querySelector('.mirror-bring__emoji')).toBeNull()
+ })
+
+ it('fits a name whose word is wider than the tile and leaves a short one alone', async () => {
+ const realComputed = window.getComputedStyle.bind(window)
+ vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(100)
+ vi.spyOn(HTMLElement.prototype, 'scrollWidth', 'get').mockImplementation(function (this: HTMLElement) {
+ if (!this.classList.contains('mirror-bring__name')) return 0
+ const size = parseFloat(this.style.fontSize) || 16
+ return Math.max(...(this.textContent ?? '').split(' ').map((w) => w.length)) * size * 0.5
+ })
+ // Only the name gets the stub; everything else keeps the real computed style.
+ vi.spyOn(window, 'getComputedStyle').mockImplementation((el, pseudo) =>
+ el instanceof HTMLElement && el.classList.contains('mirror-bring__name')
+ ? ({ fontSize: '16px', lineHeight: '19.2px' } as CSSStyleDeclaration)
+ : realComputed(el, pseudo),
+ )
+ try {
+ setup(state([item('Fünfzehnzeichen'), item('Ei')]))
+ renderWithProviders('/')
+ const long = await screen.findByText('Fünfzehnzeichen')
+ const short = await screen.findByText('Ei')
+ expect(parseFloat(long.style.fontSize)).toBeLessThan(16)
+ expect(short.style.fontSize).toBe('')
+ } finally {
+ vi.restoreAllMocks()
+ }
+ })
+
it('renders nothing when not configured', async () => {
const api = setup({ configured: false, stale: false, items: [] })
renderWithProviders('/')
diff --git a/web/src/features/modules/bring/Widget.tsx b/web/src/features/modules/bring/Widget.tsx
index d4bdeeb..ae59a2f 100644
--- a/web/src/features/modules/bring/Widget.tsx
+++ b/web/src/features/modules/bring/Widget.tsx
@@ -1,7 +1,8 @@
-import { useState } from 'react'
+import { useState, type ReactNode } from 'react'
import { useQuery } from '@tanstack/react-query'
import type { BringItem } from './api.ts'
import { dashboardBringQueryKey, fetchDashboardBring } from './api.ts'
+import { FitName } from './FitName.tsx'
import './bring.css'
export const columns = 6
@@ -13,14 +14,25 @@ function isEmojiOnly(value: string) {
return /^\p{Extended_Pictographic}️?$/u.test(value.trim())
}
-function BringIcon({ item }: { item: BringItem }) {
+/** One tile. The emoji is shown as the symbol when no picture is, and then needs no second appearance as the name. */
+function BringTile({ item }: { item: BringItem }) {
const [failed, setFailed] = useState(false)
- if (item.iconUrl && !failed) {
- return
setFailed(true)} />
- }
const name = item.name.trim()
- if (isEmojiOnly(name)) return {name}
- return {Array.from(name)[0]?.toLocaleUpperCase('de-DE')}
+ const hasPicture = Boolean(item.iconUrl) && !failed
+ const emojiSymbol = !hasPicture && isEmojiOnly(name)
+ let icon: ReactNode
+ if (hasPicture) icon =
setFailed(true)} />
+ else if (emojiSymbol) icon = {name}
+ else icon = {Array.from(name)[0]?.toLocaleUpperCase('de-DE')}
+ return (
+
+ {icon}
+
+ {!emojiSymbol && }
+ {item.specification && {item.specification}}
+
+
+ )
}
export function BringWidget() {
@@ -55,15 +67,7 @@ export function BringWidget() {
{rows.map((row, rowIndex) => (
))}
diff --git a/web/src/features/modules/bring/bring.css b/web/src/features/modules/bring/bring.css
index 498d617..6902e03 100644
--- a/web/src/features/modules/bring/bring.css
+++ b/web/src/features/modules/bring/bring.css
@@ -1,11 +1,12 @@
-/* Port of SmartMirror2's shopping list; the region already anchors it to the bottom left. */
+/* Port of SmartMirror2's shopping list; the region already anchors it to the bottom left. Only the 140px wifi code
+ shares the bottom edge, so the list may take most of the width. */
.mirror-bring {
background: transparent;
box-sizing: border-box;
- max-width: min(720px, 60vw);
+ max-width: 100%;
padding: clamp(.7rem, 1.7vw, 1rem);
pointer-events: none;
- width: min(720px, 60vw);
+ width: min(880px, 62vw);
}
/* column-reverse: the first row of items sits at the bottom edge. */
.mirror-bring__rows { display: flex; flex-direction: column-reverse; gap: clamp(.55rem, .8vw, .65rem); width: 100%; }
@@ -14,6 +15,7 @@
font-size: clamp(.9rem, 1.05vw, 1.05rem);
gap: clamp(.55rem, .8vw, .65rem);
grid-template-columns: repeat(6, minmax(0, 1fr));
+ align-items: start;
letter-spacing: .06rem;
list-style: none;
margin: 0;
@@ -22,37 +24,32 @@
}
.mirror-bring__item {
align-items: center;
- aspect-ratio: 1;
border-radius: 6px;
box-sizing: border-box;
display: flex;
flex-direction: column;
font-weight: 700;
gap: .35rem;
- justify-content: space-between;
+ justify-content: flex-start;
min-width: 0;
- overflow: hidden;
padding: .6rem .5rem;
text-align: center;
width: 100%;
- word-break: break-word;
}
-.mirror-bring__icon-slot { align-items: center; display: flex; height: clamp(64px, 5vw, 76px); justify-content: center; text-align: center; width: 100%; }
+.mirror-bring__icon-slot { align-items: center; display: flex; height: 80px; justify-content: center; text-align: center; width: 100%; }
/* Bring!'s pictures are coloured; the mirror wants them white. */
.mirror-bring__icon {
display: block;
filter: brightness(0) invert(1);
- height: clamp(48px, 4.6vw, 64px);
+ height: 80px;
object-fit: contain;
- transform: scale(1.4);
- transform-origin: center;
- width: clamp(48px, 4.6vw, 64px);
+ width: 80px;
}
-.mirror-bring__emoji { display: block; font-size: clamp(2rem, 3vw, 2.5rem); line-height: 1; transform: scale(1.4); transform-origin: center; }
+.mirror-bring__emoji { display: block; font-size: 4.4rem; line-height: 1; }
.mirror-bring__fallback {
align-items: center;
display: flex;
- font-size: clamp(2.4rem, 4vw, 3.6rem);
+ font-size: 3.6rem;
font-weight: 700;
height: 100%;
justify-content: center;
@@ -61,5 +58,9 @@
width: 100%;
}
.mirror-bring__label { align-items: center; display: flex; flex-direction: column; gap: .15rem; line-height: 1.2; min-width: 0; text-align: center; width: 100%; }
-.mirror-bring__name, .mirror-bring__specification { max-width: 100%; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
-.mirror-bring__specification { color: var(--mirror-muted); font-size: clamp(.78rem, .95vw, .9rem); font-weight: 600; }
+/* Nothing is clipped: text wraps at spaces; FitName shrinks a word wider than the tile and, as a last resort, adds --break. */
+.mirror-bring__name, .mirror-bring__specification { max-width: 100%; min-width: 0; white-space: normal; }
+/* Explicit, so nothing inherited can split a word before FitName has measured it. */
+.mirror-bring__name { overflow-wrap: normal; word-break: normal; }
+.mirror-bring__name--break { overflow-wrap: anywhere; }
+.mirror-bring__specification { color: var(--mirror-muted); font-size: clamp(.78rem, .95vw, .9rem); font-weight: 600; overflow-wrap: anywhere; }
diff --git a/web/src/features/modules/bring/bringCss.test.ts b/web/src/features/modules/bring/bringCss.test.ts
index ddddc27..4018644 100644
--- a/web/src/features/modules/bring/bringCss.test.ts
+++ b/web/src/features/modules/bring/bringCss.test.ts
@@ -25,4 +25,30 @@ describe('bring.css', () => {
expect(match).not.toBeNull()
expect(Number(match![1])).toBe(columns)
})
+
+ it('never clips a tile, its icon or its texts', () => {
+ const item = declarations('.mirror-bring__item')
+ expect(item).not.toMatch(/aspect-ratio/)
+ expect(item).not.toMatch(/overflow:\s*hidden/)
+ expect(declarations('.mirror-bring__icon')).not.toMatch(/transform:/)
+ expect(declarations('.mirror-bring__emoji')).not.toMatch(/transform:/)
+ for (const selector of ['.mirror-bring__name, .mirror-bring__specification', '.mirror-bring__specification']) {
+ const rule = declarations(selector)
+ expect(rule).not.toMatch(/nowrap|text-overflow/)
+ }
+ expect(css).not.toMatch(/nowrap|text-overflow|aspect-ratio|(? {
+ expect(declarations('.mirror-bring')).toMatch(/width:\s*min\(880px,\s*62vw\)/)
+ })
+
+ it('breaks inside a word only for the last-resort name class and the specification', () => {
+ for (const selector of ['.mirror-bring__item', '.mirror-bring__label', '.mirror-bring__list', '.mirror-bring__name']) {
+ expect(declarations(selector), selector).not.toMatch(/break-word|anywhere|break-all/)
+ }
+ expect(declarations('.mirror-bring__name')).toMatch(/overflow-wrap:\s*normal/)
+ expect(declarations('.mirror-bring__name')).toMatch(/word-break:\s*normal/)
+ expect(declarations('.mirror-bring__name--break')).toMatch(/overflow-wrap:\s*anywhere/)
+ })
})