Skip to content

Commit

Permalink
feat: update link to use element internals custom states for presenta…
Browse files Browse the repository at this point in the history
…tional styling (microsoft#31752)
  • Loading branch information
chrisdholt authored Jun 18, 2024
1 parent b101c34 commit 7428b7a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "feat: update link to use Element Internals custom states for presentational attributes",
"packageName": "@fluentui/web-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
2 changes: 2 additions & 0 deletions packages/web-components/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,9 @@ export const lineHeightHero900 = "var(--lineHeightHero900)";
// @public
export class Link extends BaseAnchor {
appearance?: LinkAppearance | undefined;
appearanceChanged(prev: LinkAppearance | undefined, next: LinkAppearance | undefined): void;
inline: boolean;
inlineChanged(prev: boolean, next: boolean): void;
}

// @public
Expand Down
2 changes: 1 addition & 1 deletion packages/web-components/src/anchor-button/anchor-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class BaseAnchor extends FASTElement {
*
* @internal
*/
protected elementInternals: ElementInternals = this.attachInternals();
public elementInternals: ElementInternals = this.attachInternals();

/**
* The proxy anchor element
Expand Down
33 changes: 33 additions & 0 deletions packages/web-components/src/link/link.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { spinalCase } from '@microsoft/fast-web-utilities';
import { expect, test } from '@playwright/test';
import { fixtureURL } from '../helpers.tests.js';
import { Link } from './link.js';

const proxyAttributes = {
href: 'href',
Expand Down Expand Up @@ -55,4 +56,36 @@ test.describe('Link', () => {
await expect(proxy).toHaveAttribute(`${attribute}`, `${value}`);
});
}

test('should add a custom state matching the `appearance` attribute when provided', async ({ page }) => {
const element = page.locator('fluent-link');

await element.evaluate((node: Link) => {
node.appearance = 'subtle';
});

expect(await element.evaluate((node: Link) => node.elementInternals.states.has('subtle'))).toBe(true);

await element.evaluate((node: Link) => {
node.appearance = undefined;
});

expect(await element.evaluate((node: Link) => node.elementInternals.states.has('subtle'))).toBe(false);
});

test('should add a custom state of `inline` when true', async ({ page }) => {
const element = page.locator('fluent-link');

await element.evaluate((node: Link) => {
node.inline = true;
});

expect(await element.evaluate((node: Link) => node.elementInternals.states.has('inline'))).toBe(true);

await element.evaluate((node: Link) => {
node.inline = false;
});

expect(await element.evaluate((node: Link) => node.elementInternals.states.has('inline'))).toBe(false);
});
});
9 changes: 5 additions & 4 deletions packages/web-components/src/link/link.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
fontWeightRegular,
strokeWidthThin,
} from '../theme/design-tokens.js';
import { subtleState } from '../styles/states/index.js';

export const styles = css`
${display('inline')}
Expand Down Expand Up @@ -49,21 +50,21 @@ export const styles = css`
color: ${colorBrandForegroundLinkPressed};
}
:host([appearance='subtle']:hover) {
:host(${subtleState}:hover) {
color: ${colorNeutralForeground2LinkHover};
}
:host([appearance='subtle']:active) {
:host(${subtleState}:active) {
color: ${colorNeutralForeground2LinkPressed};
}
}
:host([appearance='subtle']) {
:host(${subtleState}) {
color: ${colorNeutralForeground2Link};
}
:host-context(:is(h1, h2, h3, h4, h5, h6, p, fluent-text)),
:host([inline]) {
:host(:is([state--inline], :state(inline))) {
font: inherit;
text-decoration: underline;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/web-components/src/link/link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { attr } from '@microsoft/fast-element';
import { BaseAnchor } from '../anchor-button/anchor-button.js';
import { toggleState } from '../utils/element-internals.js';
import { type LinkAppearance } from './link.options.js';

/**
Expand All @@ -23,6 +24,20 @@ export class Link extends BaseAnchor {
@attr
public appearance?: LinkAppearance | undefined;

/**
* Handles changes to appearance attribute custom states
* @param prev - the previous state
* @param next - the next state
*/
public appearanceChanged(prev: LinkAppearance | undefined, next: LinkAppearance | undefined) {
if (prev) {
toggleState(this.elementInternals, `${prev}`, false);
}
if (next) {
toggleState(this.elementInternals, `${next}`, true);
}
}

/**
* The link is inline with text
* In chromium browsers, if the link is contained within a semantic
Expand All @@ -35,4 +50,13 @@ export class Link extends BaseAnchor {
*/
@attr({ mode: 'boolean' })
public inline: boolean = false;

/**
* Handles changes to inline attribute custom states
* @param prev - the previous state
* @param next - the next state
*/
public inlineChanged(prev: boolean, next: boolean) {
toggleState(this.elementInternals, 'inline', next);
}
}

0 comments on commit 7428b7a

Please sign in to comment.