@m3e/icon 1.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,163 @@
1
+ import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit";
2
+ import { customElement, property, query } from "lit/decorators.js";
3
+
4
+ import { Role } from "@m3e/core";
5
+
6
+ import { IconVariant } from "./IconVariant";
7
+ import { IconGrade } from "./IconGrade";
8
+
9
+ /**
10
+ * @summary
11
+ * A small symbol used to easily identify an action or category.
12
+ *
13
+ * @description
14
+ * The `m3e-icon` component makes it easier to use Material Symbols in your application. Material Symbols are Google's newest icons supporting
15
+ * a range of design variants. For more information, see:
16
+ * - {@link https://developers.google.com/fonts/docs/material_symbols|Material Symbol Guide}
17
+ * - {@link https://fonts.google.com/icons|Material Symbol Library}
18
+ *
19
+ * The Material Symbols font is the easiest way to incorporate Material Symbols into your application. Using the
20
+ * {@link https://developers.google.com/fonts/docs/css2#forming_api_urls|Fonts CSS API}, you can use variable fonts to optimize icon
21
+ * usage within your application. See {@link https://caniuse.com/variable-fonts|Can I Use's Variable Fonts} to determine whether
22
+ * your user's browser support variable fonts.
23
+ *
24
+ * @example
25
+ * The following example illustrates showing the `home` icon. The `name` attribute specifies the icon to present.
26
+ * ```html
27
+ * <m3e-icon name="home"></m3e-icon>
28
+ * ```
29
+ * @example
30
+ * The next example illustrates a link used to download a variable font for outlined icons with fill support.
31
+ * ```html
32
+ * <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0..1,0" rel="stylesheet"/>
33
+ * ```
34
+ *
35
+ * @tag m3e-icon
36
+ *
37
+ * @attr filled - Whether the icon is filled.
38
+ * @attr grade - The grade of the icon.
39
+ * @attr optical-size - A value from 20 to 48 indicating the optical size of the icon.
40
+ * @attr name - The name of the icon.
41
+ * @attr variant - The appearance variant of the icon.
42
+ * @attr weight - A value from 100 to 700 indicating the weight of the icon.
43
+ */
44
+ @customElement("m3e-icon")
45
+ export class M3eIconElement extends Role(LitElement, "img") {
46
+ /** The styles of the element. */
47
+ static override styles: CSSResultGroup = css`
48
+ :host {
49
+ display: inline-block;
50
+ user-select: none;
51
+ font-size: var(--m3e-icon-size, 1.5rem);
52
+ width: 1em;
53
+ vertical-align: middle;
54
+ overflow: hidden;
55
+ }
56
+ .icon {
57
+ font-weight: normal;
58
+ font-style: normal;
59
+ line-height: 1;
60
+ letter-spacing: normal;
61
+ text-transform: none;
62
+ white-space: nowrap;
63
+ word-wrap: normal;
64
+ direction: ltr;
65
+ font-feature-settings: "liga";
66
+ -webkit-font-smoothing: antialiased;
67
+ width: inherit;
68
+ height: inherit;
69
+ vertical-align: inherit;
70
+ font-variation-settings: "FILL" var(--_icon-fill, 0), "wght" var(--_icon-weight, 400),
71
+ "GRAD" var(--_icon-grade, 0), "opsz" var(--_icon-optical-size, 24);
72
+ }
73
+ :host([variant="outlined"]) .icon {
74
+ font-family: "Material Symbols Outlined";
75
+ }
76
+ :host([variant="rounded"]) .icon {
77
+ font-family: "Material Symbols Rounded";
78
+ }
79
+ :host([variant="sharp"]) .icon {
80
+ font-family: "Material Symbols Sharp";
81
+ }
82
+ `;
83
+
84
+ /** @private */ @query(".icon") private readonly _icon?: HTMLSpanElement;
85
+
86
+ /** The name of the icon. */
87
+ @property() name: string = "";
88
+
89
+ /**
90
+ * The appearance variant of the icon.
91
+ * @default "outlined"
92
+ */
93
+ @property({ reflect: true }) variant: IconVariant = "outlined";
94
+
95
+ /**
96
+ * Whether the icon is filled.
97
+ * @default false
98
+ */
99
+ @property({ type: Boolean, reflect: true }) filled = false;
100
+
101
+ /**
102
+ * The grade of the icon.
103
+ * @default "medium"
104
+ */
105
+ @property() grade: IconGrade = "medium";
106
+
107
+ /**
108
+ * A value from 100 to 700 indicating the weight of the icon.
109
+ * @default 400
110
+ */
111
+ @property({ type: Number }) weight = 400;
112
+
113
+ /**
114
+ * A value from 20 to 48 indicating the optical size of the icon.
115
+ * @default 24
116
+ */
117
+ @property({ attribute: "optical-size", type: Number }) opticalSize = 24;
118
+
119
+ /** @inheritdoc */
120
+ override connectedCallback(): void {
121
+ if (
122
+ !this.hasAttribute("aria-label") &&
123
+ !this.hasAttribute("aria-labelledby") &&
124
+ !this.hasAttribute("aria-hidden")
125
+ ) {
126
+ this.ariaHidden = "true";
127
+ }
128
+
129
+ super.connectedCallback();
130
+ }
131
+
132
+ /** @inheritdoc */
133
+ protected override updated(_changedProperties: PropertyValues<this>): void {
134
+ super.updated(_changedProperties);
135
+
136
+ if (_changedProperties.has("filled")) {
137
+ this._icon?.style.setProperty("--_icon-fill", this.filled ? "1" : "0");
138
+ }
139
+ if (_changedProperties.has("grade")) {
140
+ this._icon?.style.setProperty(
141
+ "--_icon-grade",
142
+ this.grade === "low" ? "-25" : this.grade === "high" ? "200" : "0"
143
+ );
144
+ }
145
+ if (_changedProperties.has("weight")) {
146
+ this._icon?.style.setProperty("--_icon-weight", `${this.weight}`);
147
+ }
148
+ if (_changedProperties.has("opticalSize")) {
149
+ this._icon?.style.setProperty("--_icon-optical-size", `${this.opticalSize}`);
150
+ }
151
+ }
152
+
153
+ /** @inheritdoc */
154
+ protected override render(): unknown {
155
+ return html`<div class="icon" aria-hidden="true">${this.name}</div>`;
156
+ }
157
+ }
158
+
159
+ declare global {
160
+ interface HTMLElementTagNameMap {
161
+ "m3e-icon": M3eIconElement;
162
+ }
163
+ }
@@ -0,0 +1,2 @@
1
+ /** Specifies the possible grades of an icon. */
2
+ export type IconGrade = "low" | "medium" | "high";
@@ -0,0 +1,2 @@
1
+ /** Specifies the possible appearance variants of an icon. */
2
+ export type IconVariant = "outlined" | "rounded" | "sharp";
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./IconElement";
2
+ export * from "./IconGrade";
3
+ export * from "./IconVariant";
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist/src"
6
+ },
7
+ "include": ["src/**/*.ts", "**/*.mjs", "**/*.js"],
8
+ "exclude": []
9
+ }