Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: standardization and refactor #3006

Merged
merged 8 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/web-components/fast-components/ACKNOWLEDGEMENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Acknowledgements

* A huge thank-you to [TypeScript](www.typescriptlang.org/) for providing an amazing language, build tools, and at least one [code sample](./src/utilities/apply-mixins.ts) we've shamelessly stolen.
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import { html, ref } from "@microsoft/fast-element";
import { endTemplate, startTemplate } from "../patterns/start-end";
import { Anchor } from "./anchor";

export const AnchorTemplate = html<Anchor>`
<a
class="control"
download="${x => x.download}"
href="${x => x.href}"
hreflang="${x => x.hreflang}"
ping="${x => x.ping}"
referrerpolicy="${x => x.referrerpolicy}"
rel="${x => x.rel}"
target="${x => x.target}"
type="${x => x.type}"
>
<span name="start" part="start" ${ref("startContainer")}>
<slot
name="start"
${ref("start")}
@slotchange=${x => x.handleStartContentChange()}
></slot>
</span>
<span class="content" part="content">
<slot></slot>
</span>
<span name="end" part="end" ${ref("endContainer")}>
<slot
name="end"
${ref("end")}
@slotchange=${x => x.handleEndContentChange()}
></slot>
</span>
</a>
<template class="${x => x.appearance}">
<a
class="control"
download="${x => x.download}"
href="${x => x.href}"
hreflang="${x => x.hreflang}"
ping="${x => x.ping}"
referrerpolicy="${x => x.referrerpolicy}"
rel="${x => x.rel}"
target="${x => x.target}"
type="${x => x.type}"
>
${startTemplate}
<span class="content" part="content">
<slot></slot>
</span>
${endTemplate}
</a>
</template>
`;
32 changes: 6 additions & 26 deletions packages/web-components/fast-components/src/anchor/anchor.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { attr, FASTElement } from "@microsoft/fast-element";
import { ButtonAppearance } from "../button";
import { StartEnd } from "../patterns/start-end";
import { applyMixins } from "../utilities/apply-mixins";

export type AnchorAppearance = ButtonAppearance | "hypertext";

export class Anchor extends FASTElement {
@attr
public appearance: AnchorAppearance = "neutral";
public appearanceChanged(
oldValue: AnchorAppearance,
newValue: AnchorAppearance
): void {
if (oldValue !== newValue) {
this.classList.add(newValue);
this.classList.remove(oldValue);
}
}

@attr
public download: string;
Expand All @@ -39,20 +31,8 @@ export class Anchor extends FASTElement {

@attr
public type: string;

public start: HTMLSlotElement;
public startContainer: HTMLSpanElement;
public handleStartContentChange(): void {
this.start.assignedNodes().length > 0
? this.startContainer.classList.add("start")
: this.startContainer.classList.remove("start");
}

public end: HTMLSlotElement;
public endContainer: HTMLSpanElement;
public handleEndContentChange(): void {
this.end.assignedNodes().length > 0
? this.endContainer.classList.add("end")
: this.endContainer.classList.remove("end");
}
}

/* eslint-disable-next-line */
export interface Anchor extends StartEnd {}
applyMixins(Anchor, StartEnd);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { html } from "@microsoft/fast-element";
import { Badge } from "./badge";

export const BadgeTemplate = html<Badge>`
<template>
<template class="${x => (x.circular ? "circular" : "")}">
<div
class="badge"
style="background-color: var(--badge-fill-${x =>
Expand Down
5 changes: 0 additions & 5 deletions packages/web-components/fast-components/src/badge/badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ export class Badge extends FASTElement {

@attr({ mode: "boolean" })
public circular: boolean;
private circularChanged(): void {
this.circular
? this.classList.add("circular")
: this.classList.remove("circular");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { html, ref } from "@microsoft/fast-element";
import { endTemplate, startTemplate } from "../patterns/start-end";
import { Button } from "./";

export const ButtonTemplate = html<Button>`
Expand All @@ -16,22 +17,10 @@ export const ButtonTemplate = html<Button>`
type=${x => x.type}
value=${x => x.value}
>
<span part="start" ${ref("startContainer")}>
<slot
name="start"
${ref("start")}
@slotchange=${x => x.handleStartContentChange()}
></slot>
</span>
${startTemplate}
<span class="content" part="content">
<slot></slot>
</span>
<span part="end" ${ref("endContainer")}>
<slot
name="end"
${ref("end")}
@slotchange=${x => x.handleEndContentChange()}
></slot>
</span>
${endTemplate}
</button>
`;
22 changes: 6 additions & 16 deletions packages/web-components/fast-components/src/button/button.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { attr } from "@microsoft/fast-element";
import { FormAssociated } from "../form-associated";
import { StartEnd } from "../patterns/start-end";
import { applyMixins } from "../utilities/apply-mixins";

export type ButtonAppearance =
| "accent"
Expand Down Expand Up @@ -85,22 +87,6 @@ export class Button extends FormAssociated<HTMLInputElement> {
}
}

public start: HTMLSlotElement;
public startContainer: HTMLSpanElement;
public handleStartContentChange(): void {
this.start.assignedNodes().length > 0
? this.startContainer.classList.add("start")
: this.startContainer.classList.remove("start");
}

public end: HTMLSlotElement;
public endContainer: HTMLSpanElement;
public handleEndContentChange(): void {
this.end.assignedNodes().length > 0
? this.endContainer.classList.add("end")
: this.endContainer.classList.remove("end");
}

protected proxy: HTMLInputElement = document.createElement("input");

constructor() {
Expand All @@ -115,3 +101,7 @@ export class Button extends FormAssociated<HTMLInputElement> {
this.setFormValue(this.value, this.value);
}
}

/* eslint-disable-next-line */
export interface Button extends StartEnd {}
applyMixins(Button, StartEnd);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { html } from "@microsoft/fast-element";
import { Card } from "./card";

export const CardTemplate = html<Card>` <slot></slot> `;
export const CardTemplate = html<Card>`<slot></slot>`;
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const CheckboxTemplate = html<Checkbox>`
tabindex="${x => (x.disabled ? null : 0)}"
@keypress="${(x, c) => x.keypressHandler(c.event as KeyboardEvent)}"
@click="${(x, c) => x.clickHandler(c.event as MouseEvent)}"
class="${x => (x.readOnly ? "readonly" : "")} ${x =>
x.checked ? "checked" : ""} ${x => (x.indeterminate ? "indeterminate" : "")}"
>
<div part="control" class="control">
<slot name="checked-indicator">
Expand Down
15 changes: 1 addition & 14 deletions packages/web-components/fast-components/src/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export class Checkbox extends FormAssociated<HTMLInputElement> {
if (this.proxy instanceof HTMLElement) {
this.proxy.readOnly = this.readOnly;
}

this.readOnly
? this.classList.add("readonly")
: this.classList.remove("readonly");
}

/**
Expand Down Expand Up @@ -69,12 +65,8 @@ export class Checkbox extends FormAssociated<HTMLInputElement> {
}

if (this.constructed) {
this.dispatchEvent(
new CustomEvent("change", { bubbles: true, composed: true })
);
this.$emit("change");
}

this.checked ? this.classList.add("checked") : this.classList.remove("checked");
}

protected proxy: HTMLInputElement = document.createElement("input");
Expand All @@ -84,11 +76,6 @@ export class Checkbox extends FormAssociated<HTMLInputElement> {
*/
@observable
public indeterminate: boolean = false;
private indeterminateChanged(): void {
this.indeterminate
? this.classList.add("indeterminate")
: this.classList.remove("indeterminate");
}

/**
* Tracks whether the "checked" property has been changed.
Expand Down
7 changes: 1 addition & 6 deletions packages/web-components/fast-components/src/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ export class Dialog extends FASTElement {
private observer: MutationObserver;

public dismiss(): void {
this.dispatchEvent(
new CustomEvent("dismiss", {
bubbles: true,
composed: true,
})
);
this.$emit("dismiss");
}

public connectedCallback(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { html } from "@microsoft/fast-element";
import { Divider } from "./divider";

export const DividerTemplate = html<Divider>` <template role=${x => x.role}></template> `;
export const DividerTemplate = html<Divider>`<template role=${x => x.role}></template>`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { attr, emptyArray, FASTElement } from "@microsoft/fast-element";
import { attr, DOM, emptyArray, FASTElement } from "@microsoft/fast-element";
import { keyCodeEnter } from "@microsoft/fast-web-utilities";

/**
Expand Down Expand Up @@ -182,9 +182,7 @@ export abstract class FormAssociated<
this.proxy.disabled = this.disabled;
}

this.disabled
? this.classList.add("disabled")
: this.classList.remove("disabled");
DOM.queueUpdate(() => this.classList.toggle("disabled", this.disabled));
}

@attr
Expand All @@ -205,9 +203,7 @@ export abstract class FormAssociated<
this.proxy.required = this.required;
}

this.required
? this.classList.add("required")
: this.classList.remove("required");
DOM.queueUpdate(() => this.classList.toggle("required", this.required));
}

/**
Expand Down
38 changes: 38 additions & 0 deletions packages/web-components/fast-components/src/patterns/start-end.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { html, ref } from "@microsoft/fast-element";

export class StartEnd {
public start: HTMLSlotElement;
public startContainer: HTMLSpanElement;
public handleStartContentChange(): void {
this.startContainer.classList.toggle(
"start",
this.start.assignedNodes().length > 0
);
}

public end: HTMLSlotElement;
public endContainer: HTMLSpanElement;
public handleEndContentChange(): void {
this.endContainer.classList.toggle("end", this.end.assignedNodes().length > 0);
}
}

export const endTemplate = html<StartEnd>`
<span name="end" part="end" ${ref("endContainer")}>
<slot
name="end"
${ref("end")}
@slotchange=${x => x.handleEndContentChange()}
></slot>
</span>
`;

export const startTemplate = html<StartEnd>`
<span name="start" part="start" ${ref("startContainer")}>
<slot
name="start"
${ref("start")}
@slotchange=${x => x.handleStartContentChange()}
></slot>
</span>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ export class BaseProgress extends FASTElement {

@attr({ mode: "boolean" })
public paused;
private pausedChanged(): void {
this.paused ? this.classList.add("paused") : this.classList.remove("paused");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const ProgressRingTemplate = html<BaseProgress>`
aria-valuenow="${x => x.value}"
aria-valuemin="${x => x.min}"
aria-valuemax="${x => x.max}"
class="${x => (x.paused ? "paused" : "")}"
>
${when(
x => x.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const ProgressTemplate = html<BaseProgress>`
aria-valuenow="${x => x.value}"
aria-valuemin="${x => x.min}"
aria-valuemax="${x => x.max}"
class="${x => (x.paused ? "paused" : "")}"
>
${when(
x => x.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { html, ref, when } from "@microsoft/fast-element";
import { SliderLabel } from "./slider-label";

export const SliderLabelTemplate = html<SliderLabel>`
<template aria-disabled="${x => x.disabled}">
<template
aria-disabled="${x => x.disabled}"
class="${x => (x.disabled ? "disabled" : "")}"
>
<div ${ref("root")} part="root" class="root" style=${x => x.positionStyle}>
<div class="container">
${when(x => !x.hideMark, html` <div class="mark"></div> `)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ export class SliderLabel extends FASTElement {
orientation: SliderOrientation.horizontal,
};

@attr({ attribute: "disabled", mode: "boolean" })
public disabled: boolean; // Map to proxy element
private disabledChanged(): void {
this.disabled
? this.classList.add("disabled")
: this.classList.remove("disabled");
}
@attr({ mode: "boolean" })
public disabled: boolean;

public connectedCallback(): void {
super.connectedCallback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const SliderTemplate = html<Slider>`
?aria-disabled="${x => x.disabled}"
?aria-readonly="${x => x.readOnly}"
aria-orientation="${x => x.orientation}"
class="${x => x.orientation}"
>
<div part="positioning-region" class="positioning-region">
<div ${ref("track")} part="track-container" class="track">
Expand Down
Loading