diff --git a/addon-test-support/index.js b/addon-test-support/index.js new file mode 100644 index 00000000..d08bf17c --- /dev/null +++ b/addon-test-support/index.js @@ -0,0 +1,115 @@ +import { render } from '@ember/test-helpers'; + +/** + * Test helpers for the canvas layout (`Layout::Canvas` and the pieces that + * float over it). Import from `@fleetbase/ember-ui/test-support`. + * + * The shell's colours are CSS variables from `styles/tokens.css`, switched by + * the nearest `data-theme` attribute. These helpers set that attribute on + * (where the console sets it) and put it back afterwards, so a test can + * render the same template in both themes without leaking a theme into the + * next test. + * + * @example + * import { setupShellTheme, renderShell, eachShellTheme, shellToken } from '@fleetbase/ember-ui/test-support'; + * + * module('Integration | Component | my-chrome', function (hooks) { + * setupRenderingTest(hooks); + * setupShellTheme(hooks); + * + * eachShellTheme((theme) => { + * test(`it renders in ${theme}`, async function (assert) { + * const canvas = await renderShell(hbs``, { theme }); + * assert.strictEqual(shellToken('canvas-bg', canvas), theme === 'dark' ? '#0b1220' : '#eef1f5'); + * }); + * }); + * }); + */ + +/** The themes the shell supports, in the order tests should cover them. */ +export const SHELL_THEMES = Object.freeze(['light', 'dark']); + +/** + * Sets the shell theme on . + * + * @param {'light'|'dark'} theme + * @throws {Error} for anything other than a supported theme, so a typo fails loudly instead of rendering light + */ +export function setShellTheme(theme) { + if (!SHELL_THEMES.includes(theme)) { + throw new Error(`Unknown shell theme "${theme}"; expected one of: ${SHELL_THEMES.join(', ')}`); + } + + document.body.setAttribute('data-theme', theme); +} + +/** + * Records 's current `data-theme` and returns a function that restores + * it exactly, removing the attribute if there was none. + * + * @returns {() => void} + */ +export function snapshotShellTheme() { + const previous = document.body.getAttribute('data-theme'); + + return function restoreShellTheme() { + if (previous === null) { + document.body.removeAttribute('data-theme'); + } else { + document.body.setAttribute('data-theme', previous); + } + }; +} + +/** + * Restores 's `data-theme` after every test in the module. + * + * @param {object} hooks QUnit module hooks + */ +export function setupShellTheme(hooks) { + let restore; + + hooks.beforeEach(function () { + restore = snapshotShellTheme(); + }); + + hooks.afterEach(function () { + restore(); + }); +} + +/** + * Calls `callback` once per supported theme. Use it to declare one test per + * theme, so a failure names the theme it happened in. + * + * @param {(theme: 'light'|'dark') => void} callback + */ +export function eachShellTheme(callback) { + SHELL_THEMES.forEach((theme) => callback(theme)); +} + +/** + * Sets the theme, renders `template` and returns the rendered canvas root + * (`.fb-canvas`), or `null` when the template renders no canvas. + * + * @param {object} template a compiled template (`hbs`...``) + * @param {{theme?: 'light'|'dark'}} [options] + * @returns {Promise} + */ +export async function renderShell(template, { theme = 'light' } = {}) { + setShellTheme(theme); + await render(template); + + return document.querySelector('#ember-testing .fb-canvas'); +} + +/** + * Reads a shell token's resolved value, e.g. `shellToken('canvas-bg')`. + * + * @param {string} name the token name without the `--fb-` prefix + * @param {Element} [element] where to resolve it; defaults to + * @returns {string} the trimmed value, or '' when the token is not defined + */ +export function shellToken(name, element = document.body) { + return getComputedStyle(element).getPropertyValue(`--fb-${name}`).trim(); +} diff --git a/addon/components/layout/canvas.hbs b/addon/components/layout/canvas.hbs new file mode 100644 index 00000000..5351f92c --- /dev/null +++ b/addon/components/layout/canvas.hbs @@ -0,0 +1,17 @@ +
+
+ {{#if (has-block "backdrop")}} + {{yield to="backdrop"}} + {{else}} + + {{/if}} +
+
+ {{yield}} +
+ {{#if (has-block "chrome")}} +
+ {{yield to="chrome"}} +
+ {{/if}} +
\ No newline at end of file diff --git a/addon/components/layout/canvas/backdrop/dot-grid.hbs b/addon/components/layout/canvas/backdrop/dot-grid.hbs new file mode 100644 index 00000000..40370bb4 --- /dev/null +++ b/addon/components/layout/canvas/backdrop/dot-grid.hbs @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/addon/styles/addon.css b/addon/styles/addon.css index 7397763d..7b5a9270 100644 --- a/addon/styles/addon.css +++ b/addon/styles/addon.css @@ -1,3 +1,6 @@ +/** Design tokens: CSS variables only, no rules */ +@import 'tokens.css'; + /** Layout */ @import 'layout/reset.css'; @import "ember-basic-dropdown/vendor/ember-basic-dropdown.css"; @@ -9,6 +12,7 @@ @import 'layout/utilities.css'; @import 'layout/dark-theme.css'; @import 'layout/light-theme.css'; +@import 'layout/canvas.css'; /** Components */ @import 'components/modal.css'; diff --git a/addon/styles/layout/canvas.css b/addon/styles/layout/canvas.css new file mode 100644 index 00000000..bf7809bb --- /dev/null +++ b/addon/styles/layout/canvas.css @@ -0,0 +1,52 @@ +/** + * Layout::Canvas, an opt-in application shell (the existing layout components + * do not use it). Three stacked layers, lowest first: backdrop, content, chrome. + * All colours and geometry come from `styles/tokens.css`. + */ + +.fb-canvas { + position: relative; + width: 100%; + height: 100%; + overflow: hidden; + isolation: isolate; + background-color: var(--fb-canvas-bg); + color: var(--fb-text); +} + +.fb-canvas-backdrop { + position: absolute; + inset: 0; + z-index: 0; +} + +.fb-canvas-content { + position: absolute; + top: var(--fb-inset-top); + right: var(--fb-inset-right); + bottom: var(--fb-inset-bottom); + left: var(--fb-inset-left); + z-index: 1; + overflow: auto; +} + +/* The chrome layer spans the canvas but never swallows a click meant for the + map or content below; only the chrome pieces themselves take the pointer. */ +.fb-canvas-chrome { + position: absolute; + inset: 0; + z-index: 2; + pointer-events: none; +} + +.fb-canvas-chrome > * { + pointer-events: auto; +} + +.fb-canvas-dot-grid { + position: absolute; + inset: 0; + background-color: var(--fb-canvas-bg); + background-image: radial-gradient(var(--fb-canvas-dot) var(--fb-canvas-dot-size), transparent var(--fb-canvas-dot-size)); + background-size: var(--fb-canvas-grid) var(--fb-canvas-grid); +} diff --git a/addon/styles/tokens.css b/addon/styles/tokens.css new file mode 100644 index 00000000..49a18ef0 --- /dev/null +++ b/addon/styles/tokens.css @@ -0,0 +1,106 @@ +/** + * Design tokens for the canvas layout. + * + * Every value the canvas layout needs is a CSS custom property here, so one + * `data-theme` attribute (on , or on any element such as a + * `Layout::Canvas` root) switches the whole subtree between light and dark. + * `tailwind.config.js` maps these into utilities (`bg-surface`, `text-muted`, + * `rounded-chrome`, `shadow-chrome`, ...), so templates never repeat a hex. + * + * The light values also sit on `:root` so a page without `data-theme` still + * resolves every token. All names carry the `--fb-` prefix so they cannot + * collide with host or engine variables. None of the existing components read + * them, so importing this file changes nothing until a component uses them. + */ + +:root, +[data-theme='light'] { + /* Surfaces and text */ + --fb-surface: #ffffff; + --fb-subtle: #f4f6f9; + --fb-border: #e3e8ef; + --fb-text: #0f172a; + --fb-muted: #526077; + --fb-accent: #2563eb; + --fb-accent-fg: #ffffff; + --fb-focus: #2563eb; + + /* Floating chrome (context bar, dock, tray, taskbar) */ + --fb-chrome: rgba(255, 255, 255, 0.92); + --fb-chrome-border: rgba(15, 23, 42, 0.09); + + /* Status: foreground and its tinted background */ + --fb-info: #1d4ed8; + --fb-info-subtle: #e8f0fe; + --fb-warn: #92400e; + --fb-warn-subtle: #fef3c7; + --fb-ok: #166534; + --fb-ok-subtle: #dcfce7; + --fb-critical: #b91c1c; + --fb-critical-subtle: #fee2e2; + + /* Map backdrop */ + --fb-map-land: #edf0f4; + --fb-map-water: #c9daec; + --fb-map-road: #ffffff; + --fb-map-route: #2563eb; + + /* Dot-grid canvas */ + --fb-canvas-bg: #eef1f5; + --fb-canvas-dot: #b8c3d1; + --fb-canvas-grid: 22px; + --fb-canvas-dot-size: 1px; + + /* Shadows */ + --fb-shadow-chrome: 0 8px 24px rgba(15, 23, 42, 0.1); + --fb-shadow-sheet: 0 16px 40px rgba(15, 23, 42, 0.14); +} + +[data-theme='dark'] { + --fb-surface: #0f1726; + --fb-subtle: #182235; + --fb-border: #2a3852; + --fb-text: #e7ecf4; + --fb-muted: #9aa7bd; + --fb-accent: #3b82f6; + --fb-accent-fg: #ffffff; + --fb-focus: #60a5fa; + + --fb-chrome: rgba(15, 23, 38, 0.9); + --fb-chrome-border: rgba(148, 163, 184, 0.22); + + --fb-info: #bfdbfe; + --fb-info-subtle: #172b4d; + --fb-warn: #fde68a; + --fb-warn-subtle: #422006; + --fb-ok: #bbf7d0; + --fb-ok-subtle: #052e16; + --fb-critical: #fecaca; + --fb-critical-subtle: #450a0a; + + --fb-map-land: #1b2333; + --fb-map-water: #13233a; + --fb-map-road: #2b3750; + --fb-map-route: #60a5fa; + + --fb-canvas-bg: #0b1220; + --fb-canvas-dot: #26344f; + + --fb-shadow-chrome: 0 10px 30px rgba(0, 0, 0, 0.45); + --fb-shadow-sheet: 0 18px 44px rgba(0, 0, 0, 0.5); +} + +/* Theme-independent: geometry is the same in both themes. */ +:root { + /* Radius */ + --fb-radius-control: 8px; + --fb-radius-card: 12px; + --fb-radius-chrome: 14px; + --fb-radius-dock: 18px; + + /* Content insets: where the content layer sits inside the floating chrome */ + --fb-inset-left: 82px; + --fb-inset-top: 64px; + --fb-inset-bottom: 80px; + --fb-inset-right: 12px; +} diff --git a/app/components/layout/canvas.js b/app/components/layout/canvas.js new file mode 100644 index 00000000..ce8b5544 --- /dev/null +++ b/app/components/layout/canvas.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/ember-ui/components/layout/canvas'; diff --git a/app/components/layout/canvas/backdrop/dot-grid.js b/app/components/layout/canvas/backdrop/dot-grid.js new file mode 100644 index 00000000..1a281a42 --- /dev/null +++ b/app/components/layout/canvas/backdrop/dot-grid.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/ember-ui/components/layout/canvas/backdrop/dot-grid'; diff --git a/tailwind.config.js b/tailwind.config.js index 83bbe17d..1f157bc7 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -32,6 +32,22 @@ module.exports = { DEFAULT: 'transparent', }, colors: { + // Design tokens (addon/styles/tokens.css). Each resolves a CSS variable, so + // the same class follows the nearest [data-theme] without a `dark:` variant. + surface: 'var(--fb-surface)', + subtle: 'var(--fb-subtle)', + border: 'var(--fb-border)', + text: 'var(--fb-text)', + muted: 'var(--fb-muted)', + accent: { DEFAULT: 'var(--fb-accent)', fg: 'var(--fb-accent-fg)' }, + focus: 'var(--fb-focus)', + chrome: { DEFAULT: 'var(--fb-chrome)', border: 'var(--fb-chrome-border)' }, + info: { DEFAULT: 'var(--fb-info)', subtle: 'var(--fb-info-subtle)' }, + warn: { DEFAULT: 'var(--fb-warn)', subtle: 'var(--fb-warn-subtle)' }, + ok: { DEFAULT: 'var(--fb-ok)', subtle: 'var(--fb-ok-subtle)' }, + critical: { DEFAULT: 'var(--fb-critical)', subtle: 'var(--fb-critical-subtle)' }, + map: { land: 'var(--fb-map-land)', water: 'var(--fb-map-water)', road: 'var(--fb-map-road)', route: 'var(--fb-map-route)' }, + canvas: { DEFAULT: 'var(--fb-canvas-bg)', dot: 'var(--fb-canvas-dot)' }, sky: { 100: '#e6f0fb', 200: '#bad5f5', @@ -75,7 +91,15 @@ module.exports = { 850: '#212a38', }, }, + borderRadius: { + control: 'var(--fb-radius-control)', + card: 'var(--fb-radius-card)', + chrome: 'var(--fb-radius-chrome)', + dock: 'var(--fb-radius-dock)', + }, boxShadow: { + chrome: 'var(--fb-shadow-chrome)', + sheet: 'var(--fb-shadow-sheet)', xs: '0 0 0 1px rgba(0,0,0, 0.05)', 'light-xs': '0 0 0 1px rgba(212,220,236, 0.05)', 'light-sm': '0 1px 2px 0 rgba(212,220,236, 0.05)', @@ -102,6 +126,10 @@ module.exports = { 86: '30rem', }, spacing: { + 'canvas-left': 'var(--fb-inset-left)', + 'canvas-top': 'var(--fb-inset-top)', + 'canvas-bottom': 'var(--fb-inset-bottom)', + 'canvas-right': 'var(--fb-inset-right)', 70: '18rem', 74: '22rem', 78: '26rem', diff --git a/tests/integration/components/layout/canvas-test.js b/tests/integration/components/layout/canvas-test.js new file mode 100644 index 00000000..41933c53 --- /dev/null +++ b/tests/integration/components/layout/canvas-test.js @@ -0,0 +1,143 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'dummy/tests/helpers'; +import { render, click } from '@ember/test-helpers'; +import { hbs } from 'ember-cli-htmlbars'; +import { setupShellTheme, renderShell, eachShellTheme } from '@fleetbase/ember-ui/test-support'; + +const CANVAS_BG = { light: 'rgb(238, 241, 245)', dark: 'rgb(11, 18, 32)' }; +const TEXT = { light: 'rgb(15, 23, 42)', dark: 'rgb(231, 236, 244)' }; + +// Test-only geometry is applied from JS: templates may not carry inline styles. +function applyStyle(element, styles) { + for (const [property, value] of Object.entries(styles)) { + element.style.setProperty(property, value); + } + return element; +} + +module('Integration | Component | layout/canvas', function (hooks) { + setupRenderingTest(hooks); + setupShellTheme(hooks); + + test('by default it renders the dot-grid backdrop and the content layer, and no chrome layer', async function (assert) { + await render(hbs`

Orders

`); + + assert.dom('.fb-canvas').exists(); + assert.dom('.fb-canvas > [data-canvas-layer]').exists({ count: 2 }, 'only backdrop and content render without a chrome block'); + assert.dom('.fb-canvas-backdrop .fb-canvas-dot-grid').exists('the backdrop falls back to the dot grid'); + assert.dom('main.fb-canvas-content').exists('the content layer is the main landmark'); + assert.dom('main.fb-canvas-content .page').hasText('Orders'); + assert.dom('.fb-canvas-chrome').doesNotExist(); + }); + + test('a backdrop block replaces the dot grid', async function (assert) { + await render(hbs` + + <:backdrop>
map
+ <:default>

Orders

+
+ `); + + assert.dom('.fb-canvas-backdrop .live-map').hasText('map'); + assert.dom('.fb-canvas-dot-grid').doesNotExist(); + assert.dom('.fb-canvas-content .page').hasText('Orders'); + }); + + test('a chrome block renders the chrome layer above the content', async function (assert) { + await render(hbs` + + <:chrome> + + `); + + const layers = [...this.element.querySelectorAll('.fb-canvas > [data-canvas-layer]')]; + assert.deepEqual( + layers.map((layer) => layer.dataset.canvasLayer), + ['backdrop', 'content', 'chrome'], + 'layers render lowest first' + ); + assert.deepEqual( + layers.map((layer) => getComputedStyle(layer).zIndex), + ['0', '1', '2'], + 'and stack backdrop < content < chrome' + ); + assert.dom('.fb-canvas-chrome nav.dock').hasText('dock'); + }); + + test('the chrome layer passes the pointer through except on its children', async function (assert) { + this.contentClicks = 0; + this.onContentClick = () => this.contentClicks++; + + await render(hbs` + + <:default> + <:chrome>
tray
+
+ `); + applyStyle(this.element.querySelector('.fb-canvas'), { width: '600px', height: '400px' }); + applyStyle(this.element.querySelector('.under'), { width: '100%', height: '100%' }); + applyStyle(this.element.querySelector('.tray'), { position: 'absolute', right: '0', top: '0', width: '40px', height: '40px' }); + + assert.strictEqual(getComputedStyle(this.element.querySelector('.fb-canvas-chrome')).pointerEvents, 'none'); + assert.strictEqual(getComputedStyle(this.element.querySelector('.fb-canvas-chrome .tray')).pointerEvents, 'auto'); + + const under = this.element.querySelector('.under').getBoundingClientRect(); + const hit = document.elementFromPoint(under.left + 20, under.top + 20); + assert.true(hit.classList.contains('under'), 'a point covered only by the chrome layer reaches the content'); + + await click('.under'); + assert.strictEqual(this.contentClicks, 1); + }); + + // offset* rather than getBoundingClientRect: the test container is scaled, offsets are not. + test('the content layer sits inside the canvas insets', async function (assert) { + await render(hbs``); + + const canvas = applyStyle(this.element.querySelector('.fb-canvas'), { width: '1000px', height: '600px' }); + const content = this.element.querySelector('.fb-canvas-content'); + + assert.strictEqual(content.offsetParent, canvas, 'content is positioned against the canvas'); + assert.strictEqual(content.offsetLeft, 82, 'left inset'); + assert.strictEqual(content.offsetTop, 64, 'top inset'); + assert.strictEqual(canvas.clientHeight - content.offsetTop - content.offsetHeight, 80, 'bottom inset'); + assert.strictEqual(canvas.clientWidth - content.offsetLeft - content.offsetWidth, 12, 'right inset'); + }); + + test('the insets can be overridden with the token variables', async function (assert) { + await render(hbs``); + applyStyle(this.element.querySelector('.fb-canvas'), { width: '1000px', height: '600px', '--fb-inset-left': '0px', '--fb-inset-top': '0px' }); + + const content = this.element.querySelector('.fb-canvas-content'); + + assert.strictEqual(content.offsetLeft, 0); + assert.strictEqual(content.offsetTop, 0); + }); + + test('it forwards splattributes and leaves data-theme to the page without @theme', async function (assert) { + await render(hbs``); + + assert.dom('.fb-canvas').hasClass('console-shell'); + assert.dom('.fb-canvas').hasAttribute('data-test-canvas', 'yes'); + assert.dom('.fb-canvas').doesNotHaveAttribute('data-theme'); + }); + + eachShellTheme((theme) => { + test(`it renders with the ${theme} tokens from the page theme`, async function (assert) { + const canvas = await renderShell(hbs``, { theme }); + + assert.strictEqual(getComputedStyle(canvas).backgroundColor, CANVAS_BG[theme]); + assert.strictEqual(getComputedStyle(canvas).color, TEXT[theme]); + }); + + test(`@theme="${theme}" scopes the ${theme} tokens to the canvas whatever the page theme`, async function (assert) { + this.theme = theme; + const other = theme === 'dark' ? 'light' : 'dark'; + + const canvas = await renderShell(hbs``, { theme: other }); + + assert.dom(canvas).hasAttribute('data-theme', theme); + assert.strictEqual(getComputedStyle(canvas).backgroundColor, CANVAS_BG[theme]); + assert.strictEqual(getComputedStyle(canvas.querySelector('.fb-canvas-dot-grid')).backgroundColor, CANVAS_BG[theme], 'the backdrop follows the canvas theme'); + }); + }); +}); diff --git a/tests/integration/components/layout/canvas/backdrop/dot-grid-test.js b/tests/integration/components/layout/canvas/backdrop/dot-grid-test.js new file mode 100644 index 00000000..2dfac690 --- /dev/null +++ b/tests/integration/components/layout/canvas/backdrop/dot-grid-test.js @@ -0,0 +1,56 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'dummy/tests/helpers'; +import { render } from '@ember/test-helpers'; +import { hbs } from 'ember-cli-htmlbars'; +import { setupShellTheme, renderShell, eachShellTheme } from '@fleetbase/ember-ui/test-support'; + +const COLORS = { + light: { bg: 'rgb(238, 241, 245)', dot: 'rgb(184, 195, 209)' }, + dark: { bg: 'rgb(11, 18, 32)', dot: 'rgb(38, 52, 79)' }, +}; + +module('Integration | Component | layout/canvas/backdrop/dot-grid', function (hooks) { + setupRenderingTest(hooks); + setupShellTheme(hooks); + + test('it is decorative and fills its container', async function (assert) { + await render(hbs`
`); + Object.assign(this.element.querySelector('.frame').style, { position: 'relative', width: '300px', height: '200px' }); + + assert.dom('.fb-canvas-dot-grid').hasAttribute('aria-hidden', 'true'); + const grid = this.element.querySelector('.fb-canvas-dot-grid'); + assert.strictEqual(grid.offsetWidth, 300); + assert.strictEqual(grid.offsetHeight, 200); + }); + + test('it repeats the dot every 22px', async function (assert) { + await render(hbs``); + + assert.strictEqual(getComputedStyle(this.element.querySelector('.fb-canvas-dot-grid')).backgroundSize, '22px 22px'); + }); + + test('it forwards splattributes', async function (assert) { + await render(hbs``); + + assert.dom('.fb-canvas-dot-grid').hasClass('dense'); + assert.dom('.fb-canvas-dot-grid').hasAttribute('data-test-grid', 'yes'); + }); + + test('the grid size follows --fb-canvas-grid', async function (assert) { + await render(hbs``); + this.element.querySelector('.fb-canvas-dot-grid').style.setProperty('--fb-canvas-grid', '11px'); + + assert.strictEqual(getComputedStyle(this.element.querySelector('.fb-canvas-dot-grid')).backgroundSize, '11px 11px'); + }); + + eachShellTheme((theme) => { + test(`it paints the ${theme} canvas and dot colours`, async function (assert) { + await renderShell(hbs``, { theme }); + + const style = getComputedStyle(this.element.querySelector('.fb-canvas-dot-grid')); + assert.strictEqual(style.backgroundColor, COLORS[theme].bg); + assert.true(style.backgroundImage.startsWith('radial-gradient('), 'dots are a radial gradient'); + assert.true(style.backgroundImage.includes(COLORS[theme].dot), `dots use the ${theme} dot colour`); + }); + }); +}); diff --git a/tests/integration/components/layout/sidebar/navigator-test.js b/tests/integration/components/layout/sidebar/navigator-test.js index 01dc0081..081d3f8c 100644 --- a/tests/integration/components/layout/sidebar/navigator-test.js +++ b/tests/integration/components/layout/sidebar/navigator-test.js @@ -597,7 +597,11 @@ module('Integration | Component | layout/sidebar/navigator', function (hooks) { assert.dom('.next-sidebar-navigator-search-result').exists({ count: 12 }); assert.dom('.next-sidebar-navigator-search-result:first-of-type').hasClass('is-active'); - assert.dom('.next-sidebar-navigator-search-result').hasStyle({ alignItems: 'flex-start' }); + // Chrome 158 serialises a computed `align-items: flex-start` as `flow-start`; earlier versions as `flex-start`. + assert.true( + ['flex-start', 'flow-start'].includes(getComputedStyle(document.querySelector('.next-sidebar-navigator-search-result')).alignItems), + 'search results align their content to the top' + ); assert.dom('.next-sidebar-navigator-search-result-icon').hasStyle({ marginTop: '2px' }); await triggerKeyEvent('.next-sidebar-navigator-search-popover-input input', 'keydown', 'ArrowDown'); diff --git a/tests/integration/styles/tokens-test.js b/tests/integration/styles/tokens-test.js new file mode 100644 index 00000000..cf81e739 --- /dev/null +++ b/tests/integration/styles/tokens-test.js @@ -0,0 +1,74 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'dummy/tests/helpers'; +import { setupShellTheme, setShellTheme, eachShellTheme, shellToken } from '@fleetbase/ember-ui/test-support'; + +// The values each theme must resolve, as written in addon/styles/tokens.css. +const THEMED = { + surface: ['#ffffff', '#0f1726'], + subtle: ['#f4f6f9', '#182235'], + border: ['#e3e8ef', '#2a3852'], + text: ['#0f172a', '#e7ecf4'], + muted: ['#526077', '#9aa7bd'], + accent: ['#2563eb', '#3b82f6'], + 'accent-fg': ['#ffffff', '#ffffff'], + focus: ['#2563eb', '#60a5fa'], + chrome: ['rgba(255, 255, 255, 0.92)', 'rgba(15, 23, 38, 0.9)'], + 'chrome-border': ['rgba(15, 23, 42, 0.09)', 'rgba(148, 163, 184, 0.22)'], + info: ['#1d4ed8', '#bfdbfe'], + 'info-subtle': ['#e8f0fe', '#172b4d'], + warn: ['#92400e', '#fde68a'], + 'warn-subtle': ['#fef3c7', '#422006'], + ok: ['#166534', '#bbf7d0'], + 'ok-subtle': ['#dcfce7', '#052e16'], + critical: ['#b91c1c', '#fecaca'], + 'critical-subtle': ['#fee2e2', '#450a0a'], + 'map-land': ['#edf0f4', '#1b2333'], + 'map-water': ['#c9daec', '#13233a'], + 'map-road': ['#ffffff', '#2b3750'], + 'map-route': ['#2563eb', '#60a5fa'], + 'canvas-bg': ['#eef1f5', '#0b1220'], + 'canvas-dot': ['#b8c3d1', '#26344f'], + 'shadow-chrome': ['0 8px 24px rgba(15, 23, 42, 0.1)', '0 10px 30px rgba(0, 0, 0, 0.45)'], + 'shadow-sheet': ['0 16px 40px rgba(15, 23, 42, 0.14)', '0 18px 44px rgba(0, 0, 0, 0.5)'], +}; + +// Geometry is shared by both themes. +const SHARED = { + 'canvas-grid': '22px', + 'canvas-dot-size': '1px', + 'radius-control': '8px', + 'radius-card': '12px', + 'radius-chrome': '14px', + 'radius-dock': '18px', + 'inset-left': '82px', + 'inset-top': '64px', + 'inset-bottom': '80px', + 'inset-right': '12px', +}; + +module('Integration | Styles | tokens', function (hooks) { + setupRenderingTest(hooks); + setupShellTheme(hooks); + + test('without data-theme every token resolves to its light value', function (assert) { + document.body.removeAttribute('data-theme'); + + for (const [name, [light]] of Object.entries(THEMED)) { + assert.strictEqual(shellToken(name), light, name); + } + }); + + eachShellTheme((theme) => { + test(`every token resolves in the ${theme} theme`, function (assert) { + setShellTheme(theme); + const index = theme === 'dark' ? 1 : 0; + + for (const [name, values] of Object.entries(THEMED)) { + assert.strictEqual(shellToken(name), values[index], name); + } + for (const [name, value] of Object.entries(SHARED)) { + assert.strictEqual(shellToken(name), value, name); + } + }); + }); +}); diff --git a/tests/node/tokens-tailwind-test.cjs b/tests/node/tokens-tailwind-test.cjs new file mode 100644 index 00000000..4594d4b2 --- /dev/null +++ b/tests/node/tokens-tailwind-test.cjs @@ -0,0 +1,77 @@ +/* eslint-env node */ +'use strict'; + +// Run with: node --test tests/node +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { createRequire } = require('node:module'); +const { test } = require('node:test'); +const postcss = createRequire(require.resolve('broccoli-postcss-single'))('postcss'); +const tailwind = require('tailwindcss'); + +const tokensCss = fs.readFileSync(path.resolve(__dirname, '../../addon/styles/tokens.css'), 'utf8'); +const definedTokens = new Set([...tokensCss.matchAll(/(--fb-[a-z0-9-]+)\s*:/g)].map((match) => match[1])); + +async function utilities(classes) { + const config = { ...require('../../tailwind.config'), content: [{ raw: `
`, extension: 'html' }], corePlugins: { preflight: false } }; + const result = await postcss([tailwind(config)]).process('@tailwind utilities;', { from: undefined }); + return result.css; +} + +test('token utilities compile to the token variables', async () => { + const css = await utilities( + 'bg-surface text-muted border-border bg-subtle text-text bg-accent text-accent-fg bg-chrome border-chrome-border text-info bg-info-subtle text-warn text-ok text-critical bg-map-land bg-canvas rounded-chrome rounded-dock shadow-chrome shadow-sheet pl-canvas-left top-canvas-top' + ); + + for (const [selector, variable] of [ + ['.bg-surface', '--fb-surface'], + ['.text-muted', '--fb-muted'], + ['.border-border', '--fb-border'], + ['.bg-subtle', '--fb-subtle'], + ['.text-text', '--fb-text'], + ['.bg-accent', '--fb-accent'], + ['.text-accent-fg', '--fb-accent-fg'], + ['.bg-chrome', '--fb-chrome'], + ['.border-chrome-border', '--fb-chrome-border'], + ['.text-info', '--fb-info'], + ['.bg-info-subtle', '--fb-info-subtle'], + ['.text-warn', '--fb-warn'], + ['.text-ok', '--fb-ok'], + ['.text-critical', '--fb-critical'], + ['.bg-map-land', '--fb-map-land'], + ['.bg-canvas', '--fb-canvas-bg'], + ['.rounded-chrome', '--fb-radius-chrome'], + ['.rounded-dock', '--fb-radius-dock'], + ['.pl-canvas-left', '--fb-inset-left'], + ['.top-canvas-top', '--fb-inset-top'], + ]) { + const rule = css.match(new RegExp(`\\${selector} \\{([^}]*)\\}`)); + assert.ok(rule, `${selector} is generated`); + assert.ok(rule[1].includes(`var(${variable})`), `${selector} uses ${variable}`); + } + assert.match(css, /\.shadow-chrome \{[^}]*var\(--fb-shadow-chrome\)/); + assert.match(css, /\.shadow-sheet \{[^}]*var\(--fb-shadow-sheet\)/); +}); + +test('every variable the Tailwind config references is defined in tokens.css', () => { + const configSource = fs.readFileSync(path.resolve(__dirname, '../../tailwind.config.js'), 'utf8'); + const referenced = [...configSource.matchAll(/var\((--fb-[a-z0-9-]+)\)/g)].map((match) => match[1]); + + assert.ok(referenced.length > 0); + for (const variable of referenced) { + assert.ok(definedTokens.has(variable), `${variable} is defined in tokens.css`); + } +}); + +test('the dark theme overrides every themed light token', () => { + const block = (selector) => tokensCss.slice(tokensCss.indexOf(selector)).split('}')[0]; + const names = (css) => new Set([...css.matchAll(/(--fb-[a-z0-9-]+)\s*:/g)].map((match) => match[1])); + const light = names(block("[data-theme='light'] {")); + const dark = names(block("[data-theme='dark'] {")); + const geometry = new Set(['--fb-canvas-grid', '--fb-canvas-dot-size']); + + for (const name of light) { + if (!geometry.has(name)) assert.ok(dark.has(name), `${name} has a dark value`); + } +}); diff --git a/tests/unit/test-support/shell-test.js b/tests/unit/test-support/shell-test.js new file mode 100644 index 00000000..6568452b --- /dev/null +++ b/tests/unit/test-support/shell-test.js @@ -0,0 +1,102 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'dummy/tests/helpers'; +import { hbs } from 'ember-cli-htmlbars'; +import { SHELL_THEMES, setShellTheme, snapshotShellTheme, setupShellTheme, eachShellTheme, renderShell, shellToken } from '@fleetbase/ember-ui/test-support'; + +module('Unit | test-support | shell', function (hooks) { + setupRenderingTest(hooks); + setupShellTheme(hooks); + + test('SHELL_THEMES lists light then dark and cannot be changed', function (assert) { + assert.deepEqual([...SHELL_THEMES], ['light', 'dark']); + assert.true(Object.isFrozen(SHELL_THEMES)); + }); + + test('setShellTheme sets the theme on ', function (assert) { + setShellTheme('dark'); + assert.strictEqual(document.body.getAttribute('data-theme'), 'dark'); + + setShellTheme('light'); + assert.strictEqual(document.body.getAttribute('data-theme'), 'light'); + }); + + test('setShellTheme rejects an unknown theme', function (assert) { + assert.throws(() => setShellTheme('dim'), /Unknown shell theme "dim"; expected one of: light, dark/); + }); + + test('snapshotShellTheme restores a theme that was set', function (assert) { + document.body.setAttribute('data-theme', 'dark'); + const restore = snapshotShellTheme(); + + setShellTheme('light'); + restore(); + + assert.strictEqual(document.body.getAttribute('data-theme'), 'dark'); + }); + + test('snapshotShellTheme removes the theme when there was none', function (assert) { + document.body.removeAttribute('data-theme'); + const restore = snapshotShellTheme(); + + setShellTheme('dark'); + restore(); + + assert.false(document.body.hasAttribute('data-theme')); + }); + + test('setupShellTheme registers a snapshot before and a restore after each test', function (assert) { + const registered = {}; + setupShellTheme({ beforeEach: (fn) => (registered.beforeEach = fn), afterEach: (fn) => (registered.afterEach = fn) }); + + document.body.setAttribute('data-theme', 'light'); + registered.beforeEach(); + setShellTheme('dark'); + registered.afterEach(); + + assert.strictEqual(document.body.getAttribute('data-theme'), 'light'); + }); + + test('eachShellTheme calls back once per theme, in order', function (assert) { + const seen = []; + eachShellTheme((theme) => seen.push(theme)); + + assert.deepEqual(seen, ['light', 'dark']); + }); + + test('renderShell defaults to light and returns the canvas root', async function (assert) { + const canvas = await renderShell(hbs``); + + assert.strictEqual(document.body.getAttribute('data-theme'), 'light'); + assert.true(canvas.classList.contains('fb-canvas')); + }); + + test('renderShell sets the requested theme', async function (assert) { + await renderShell(hbs``, { theme: 'dark' }); + + assert.strictEqual(document.body.getAttribute('data-theme'), 'dark'); + }); + + test('renderShell returns null when the template renders no canvas', async function (assert) { + const canvas = await renderShell(hbs`

no shell

`); + + assert.strictEqual(canvas, null); + assert.dom('p').hasText('no shell'); + }); + + test('shellToken reads from by default', function (assert) { + setShellTheme('dark'); + + assert.strictEqual(shellToken('canvas-bg'), '#0b1220'); + }); + + test('shellToken reads from the given element', async function (assert) { + setShellTheme('dark'); + const canvas = await renderShell(hbs``, { theme: 'dark' }); + + assert.strictEqual(shellToken('canvas-bg', canvas), '#eef1f5'); + }); + + test('shellToken returns an empty string for an unknown token', function (assert) { + assert.strictEqual(shellToken('no-such-token'), ''); + }); +});