Skip to content

Commit

Permalink
feat(web-components): enabling setting theme on an individual element…
Browse files Browse the repository at this point in the history
… and unsetting themes (#31973)

Co-authored-by: Martin Hochel <[email protected]>
  • Loading branch information
marchbox and Hotell authored Aug 19, 2024
1 parent b30f776 commit 07f70bd
Show file tree
Hide file tree
Showing 11 changed files with 726 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "feat: enabling setting theme on an individual element and unsetting themes",
"packageName": "@fluentui/web-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
17 changes: 14 additions & 3 deletions packages/web-components/.storybook/preview.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { switchTheme } from '../public/theme-switch.js';
import { teamsDarkTheme, teamsLightTheme, webDarkTheme, webLightTheme } from '@fluentui/tokens';
import webcomponentsTheme from './theme.mjs';
import { setTheme } from '../src/theme/set-theme.js';

import '../src/index-rollup.js';
import './docs-root.css';

const themes = {
'web-light': webLightTheme,
'web-dark': webDarkTheme,
'teams-light': teamsLightTheme,
'teams-dark': teamsDarkTheme,
};

function changeTheme(/** @type {Event} */ e) {
switchTheme(/** @type {Parameters<typeof switchTheme>[number]} */ (/** @type {HTMLInputElement}*/ (e.target).value));
setTheme(themes[/** @type {keyof themes} */ (/** @type {HTMLInputElement}*/ (e.target).value)]);
}

// This is needed in Playwright.
Object.defineProperty(window, 'setTheme', { value: setTheme });

document.getElementById('theme-switch')?.addEventListener('change', changeTheme, false);
switchTheme('web-light');
setTheme(themes['web-light']);

export const parameters = {
layout: 'fullscreen',
Expand Down
79 changes: 71 additions & 8 deletions packages/web-components/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,62 @@ export class BaseButton extends FASTElement {
value?: string;
}

// @public
export class BaseCheckbox extends FASTElement {
autofocus: boolean;
get checked(): boolean;
set checked(next: boolean);
checkValidity(): boolean;
// @internal
clickHandler(e: MouseEvent): boolean | void;
// (undocumented)
connectedCallback(): void;
disabled?: boolean;
disabledAttribute?: boolean;
// @internal
protected disabledAttributeChanged(prev: boolean | undefined, next: boolean | undefined): void;
// @internal
protected disabledChanged(prev: boolean | undefined, next: boolean | undefined): void;
// @internal
elementInternals: ElementInternals;
get form(): HTMLFormElement | null;
static formAssociated: boolean;
formAttribute?: string;
// @internal
formResetCallback(): void;
initialChecked?: boolean;
// @internal
protected initialCheckedChanged(prev: boolean | undefined, next: boolean | undefined): void;
initialValue: string;
// @internal
protected initialValueChanged(prev: string, next: string): void;
// @internal
inputHandler(e: InputEvent): boolean | void;
// @internal
keydownHandler(e: KeyboardEvent): boolean | void;
// @internal
keyupHandler(e: KeyboardEvent): boolean | void;
get labels(): ReadonlyArray<HTMLLabelElement>;
name: string;
reportValidity(): boolean;
required: boolean;
// @internal
protected requiredChanged(prev: boolean, next: boolean): void;
// @internal
protected setAriaChecked(value?: boolean): void;
setCustomValidity(message: string): void;
// @internal
setFormValue(value: File | string | FormData | null, state?: File | string | FormData | null): void;
// @internal
setValidity(flags?: Partial<ValidityState>, message?: string, anchor?: HTMLElement): void;
toggleChecked(force?: boolean): void;
get validationMessage(): string;
get validity(): ValidityState;
get value(): string;
set value(value: string);
get willValidate(): boolean;
}

// @public
export class BaseDivider extends FASTElement {
// (undocumented)
Expand Down Expand Up @@ -883,8 +939,6 @@ export const ButtonType: {
// @public
export type ButtonType = ValuesOf<typeof ButtonType>;

// Warning: (ae-forgotten-export) The symbol "BaseCheckbox" needs to be exported by the entry point index.d.ts
//
// @public
export class Checkbox extends BaseCheckbox {
constructor();
Expand Down Expand Up @@ -2234,7 +2288,10 @@ export const DividerDefinition: FASTElementDefinition<typeof Divider>;

// @public
export const DividerOrientation: {
readonly horizontal: "horizontal";
readonly horizontal: "horizontal"; /**
* Divider roles
* @public
*/
readonly vertical: "vertical";
};

Expand Down Expand Up @@ -2971,7 +3028,10 @@ export const RadioGroupDefinition: FASTElementDefinition<typeof RadioGroup>;

// @public
export const RadioGroupOrientation: {
readonly horizontal: "horizontal";
readonly horizontal: "horizontal"; /**
* Radio Group orientation
* @public
*/
readonly vertical: "vertical";
};

Expand Down Expand Up @@ -3049,12 +3109,12 @@ export const roleForMenuItem: {
// Warning: (ae-internal-missing-underscore) The name "setTheme" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal
export const setTheme: (theme: Theme) => void;
export function setTheme(theme: Theme | null, node?: Document | HTMLElement): void;

// Warning: (ae-internal-missing-underscore) The name "setThemeFor" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal (undocumented)
export const setThemeFor: (element: HTMLElement, theme: Theme) => void;
// @internal @deprecated (undocumented)
export function setThemeFor(element: HTMLElement, theme: Theme | null): void;

// @public
export const shadow16 = "var(--shadow16)";
Expand Down Expand Up @@ -3475,7 +3535,10 @@ export const TablistDefinition: FASTElementDefinition<typeof Tablist>;

// @public
export const TablistOrientation: {
readonly horizontal: "horizontal";
readonly horizontal: "horizontal"; /**
* The appearance of the component
* @public
*/
readonly vertical: "vertical";
};

Expand Down
19 changes: 18 additions & 1 deletion packages/web-components/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';

const config: PlaywrightTestConfig = {
reporter: 'list',
testMatch: /.*\.spec\.ts$/,
retries: 3,
fullyParallel: process.env.CI ? false : true,
timeout: process.env.CI ? 10000 : 30000,
Expand All @@ -13,6 +13,23 @@ const config: PlaywrightTestConfig = {
width: 1280,
},
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
testMatch: /.*\.spec\.ts$/,
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
testMatch: [/set-theme\.spec\.ts$/],
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
testMatch: [/set-theme\.spec\.ts$/],
},
],
webServer: {
// double-quotes are required for Windows
command: `node -e "import('express').then(({ default: e }) => e().use(e.static('./dist/storybook')).listen(6006))"`,
Expand Down
13 changes: 0 additions & 13 deletions packages/web-components/public/theme-switch.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const tests: Record<string, TestRenderFunction> = {
setTheme(teamsDarkTheme);
setTheme(teamsLightTheme);

// Unset themes
setTheme(null);

endMeasure();

onComplete();
Expand Down
36 changes: 36 additions & 0 deletions packages/web-components/src/theme/set-theme.local.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { measurePerformance, type TestRenderFunction } from '@tensile-perf/web-components';
import { teamsDarkTheme, teamsLightTheme, webDarkTheme, webLightTheme } from '@fluentui/tokens';

import { setTheme } from './set-theme.js';

const tests: Record<string, TestRenderFunction> = {
mount: ({ onComplete }) => {
const { startMeasure, endMeasure } = measurePerformance();

const el = document.createElement('div');
document.body.append(el);

startMeasure();

// Newly set themes
setTheme(webLightTheme, el);
setTheme(webDarkTheme, el);
setTheme(teamsDarkTheme, el);
setTheme(teamsLightTheme, el);

// Cached themes
setTheme(webLightTheme, el);
setTheme(webDarkTheme, el);
setTheme(teamsDarkTheme, el);
setTheme(teamsLightTheme, el);

// Unset themes
setTheme(null, el);

endMeasure();

onComplete();
},
};

export { tests };
38 changes: 38 additions & 0 deletions packages/web-components/src/theme/set-theme.shadow.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { measurePerformance, type TestRenderFunction } from '@tensile-perf/web-components';
import { teamsDarkTheme, teamsLightTheme, webDarkTheme, webLightTheme } from '@fluentui/tokens';

import { setTheme } from './set-theme.js';

const tests: Record<string, TestRenderFunction> = {
mount: ({ onComplete }) => {
const { startMeasure, endMeasure } = measurePerformance();

const el = document.createElement('div');
el.attachShadow({ mode: 'open' });
el.shadowRoot!.append(document.createElement('span'));
document.body.append(el);

startMeasure();

// Newly set themes
setTheme(webLightTheme, el);
setTheme(webDarkTheme, el);
setTheme(teamsDarkTheme, el);
setTheme(teamsLightTheme, el);

// Cached themes
setTheme(webLightTheme, el);
setTheme(webDarkTheme, el);
setTheme(teamsDarkTheme, el);
setTheme(teamsLightTheme, el);

// Unset themes
setTheme(null, el);

endMeasure();

onComplete();
},
};

export { tests };
Loading

0 comments on commit 07f70bd

Please sign in to comment.