diff --git a/.gitignore b/.gitignore index b0a6ab872..aa943f40d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ _site package.json package-lock.json dist -docs/assets/images/sprite.svg docs/public/pagefind node_modules src/react diff --git a/docs/docs/components/icon.md b/docs/docs/components/icon.md index 657ba8017..f2ce74578 100644 --- a/docs/docs/components/icon.md +++ b/docs/docs/components/icon.md @@ -4,24 +4,37 @@ description: Icons are symbols that can be used to represent various options wit layout: component.njk --- -Web Awesome comes bundled with over 1,500 icons courtesy of the [Bootstrap Icons](https://icons.getbootstrap.com/) project. These icons are part of the `default` icon library. If you prefer, you can register [custom icon libraries](#icon-libraries) as well. +Web Awesome comes bundled with over 2,000 free icons courtesy of [Font Awesome](https://fontawesome.com/). These icons are part of the `default` icon library. Font Awesome Pro users can unlock additional icon families. Or, if you prefer, you can register your own [custom icon library](#icon-library). :::info -Depending on how you're loading Web Awesome, you may need to copy icon assets and/or [set the base path](/getting-started/installation/#setting-the-base-path) so Web Awesome knows where to load them from. Otherwise, icons may not appear and you'll see 404 Not Found errors in the dev console. +Not sure which icon to use? [Find the perfect icon over at Font Awesome!](https://fontawesome.com/search?o=r&m=free&f=brands%2Cclassic) ::: -## Default Icons - -All available icons in the `default` icon library are shown below. Click or tap on any icon to copy its name, then you can use it in your HTML like this. - -```html - -``` - ## Examples -TODO - show how to use `family` and `variant` attributes. -TODO - show how to use FA pro via `data-webawesome-kit="..."` +### Families & Variants + +The default icon library is Font Awesome Free, which comes with two icon families: `classic` and `brands`. Use the `family` attribute to set the icon family. + +Many Font Awesome Pro icon families have variants such as `thin`, `light`, `regular`, and `solid`. Font Awesome Pro users can [provide their kit code](/docs/installation) to unlock additional families, including `sharp` and `duotone`. For these icon families, use the `variant` attribute to set the variant. + +```html {.example} + + +``` + +{% raw %} +```jsx {.react} +import WaIcon from '@shoelace-style/shoelace/dist/react/icon'; + +const App = () => ( + <> + + + +); +``` +{% endraw %} ### Colors @@ -95,22 +108,12 @@ Icons are sized relative to the current font size. To change their size, set the ```html {.example}
- - - - - - - - - -
``` @@ -141,6 +144,36 @@ const App = () => ( ``` {% endraw %} +### Fixed Width Icons + +By default, icons have a 1em height and a variable width. Use the `fixed-width` attribute to render the host element in a 1em by 1em box. + +```html {.example} + + + + + + +``` + +{% raw %} +```jsx {.react} +import WaIcon from '@shoelace-style/shoelace/dist/react/icon'; + +const App = () => ( + <> + + + + + + + +); +``` +{% endraw %} + ### Labels For non-decorative icons, use the `label` attribute to announce it to assistive devices. @@ -162,14 +195,14 @@ const App = () => ; Custom icons can be loaded individually with the `src` attribute. Only SVGs on a local or CORS-enabled endpoint are supported. If you're using more than one custom icon, it might make sense to register a [custom icon library](#icon-libraries). ```html {.example} - + ``` {% raw %} ```jsx {.react} import WaIcon from '@shoelace-style/shoelace/dist/react/icon'; -const App = () => ; +const App = () => ; ``` {% endraw %} @@ -177,9 +210,9 @@ const App = () => ` component through icon libraries. Icon files can exist locally or on a CORS-enabled endpoint (e.g. a CDN). There is no limit to how many icon libraries you can register and there is no cost associated with registering them, as individual icons are only requested when they're used. -Web Awesome ships with two built-in icon libraries, `default` and `system`. The [default icon library](#customizing-the-default-library) contains all of the icons in the Bootstrap Icons project. The [system icon library](#customizing-the-system-library) contains only a small subset of icons that are used internally by Web Awesome components. +Web Awesome ships with two built-in icon libraries, `default` and `system`. The [default icon library](#customizing-the-default-library) is provided courtesy of [Font Awesome](https://fontawesome.com/). The [system icon library](#customizing-the-system-library) contains only a small subset of icons that are used internally by Web Awesome components. -To register an additional icon library, use the `registerIconLibrary()` function that's exported from `utilities/icon-library.js`. At a minimum, you must provide a name and a resolver function. The resolver function translates an icon name to a URL where the corresponding SVG file exists. Refer to the examples below to better understand how it works. +To register an additional icon library, use the `registerIconLibrary()` function that's exported from `dist/webawesome.js`. At a minimum, you must provide a name and a resolver function. The resolver function translates an icon name to a URL where the corresponding SVG file exists. Refer to the examples below to better understand how it works. If necessary, a mutator function can be used to mutate the SVG element before rendering. This is necessary for some libraries due to the many possible ways SVGs are crafted. For example, icons should ideally inherit the current text color via `currentColor`, so you may need to apply `fill="currentColor` or `stroke="currentColor"` to the SVG element using this function. @@ -187,10 +220,10 @@ Here's an example that registers an icon library located in the `/assets/icons` ```html @@ -207,6 +240,25 @@ If an icon is used before registration occurs, it will be empty initially but sh The following examples demonstrate how to register a number of popular, open source icon libraries via CDN. Feel free to adapt the code as you see fit to use your own origin or naming conventions. +### Bootstrap Icons + +This will register the [Bootstrap Icons](https://icons.getbootstrap.com/) library using the jsDelivr CDN. This library has two families: `regular` and `filled`. + +Icons in this library are licensed under the [MIT License](https://github.com/twbs/icons/blob/main/LICENSE). + +```html + +``` + ### Boxicons This will register the [Boxicons](https://boxicons.com/) library using the jsDelivr CDN. This library has three variations: regular (`bx-*`), solid (`bxs-*`), and logos (`bxl-*`). A mutator function is required to set the SVG's `fill` to `currentColor`. @@ -215,9 +267,9 @@ Icons in this library are licensed under the [Creative Commons 4.0 License](http ```html {.example} ``` -### Font Awesome - -This will register the [Font Awesome Free](https://fontawesome.com/) library using the jsDelivr CDN. This library has three variations: regular (`far-*`), solid (`fas-*`), and brands (`fab-*`). A mutator function is required to set the SVG's `fill` to `currentColor`. - -Icons in this library are licensed under the [Font Awesome Free License](https://github.com/FortAwesome/Font-Awesome/blob/master/LICENSE.txt). Some of the icons that appear on the Font Awesome website require a license and are therefore not available in the CDN. - -```html {.example} - - -
- - - - - - -
- - - - - - -
- - - - - - -
-``` - ### Heroicons This will register the [Heroicons](https://heroicons.com/) library using the jsDelivr CDN. @@ -331,7 +337,7 @@ Icons in this library are licensed under the [MIT License](https://github.com/ta ```html {.example} ``` @@ -623,15 +632,15 @@ To improve performance you can use a SVG sprites to avoid multiple trips for eac As always, make sure to benchmark these changes. When using HTTP/2, it may in fact be more bandwidth-friendly to use multiple small requests instead of 1 large sprite sheet. -:::danger +:::warning When using sprite sheets, the `wa-load` and `wa-error` events will not fire. For security reasons, browsers may apply the same-origin policy on `` elements located in the `` shadow DOM and may refuse to load a cross-origin URL. There is currently no defined way to set a cross-origin policy for `` elements. For this reason, sprite sheets should only be used if you're self-hosting them. ::: -```html {.example} +```html - -
- - -
``` ### Customizing the System Library @@ -654,7 +658,7 @@ If you want to change the icons Web Awesome uses internally, you can register an ```html + - ``` @@ -116,7 +115,7 @@ Most of the magic behind assets is handled internally by Web Awesome, but if you ```html ``` +## Setting a Kit Code + +Font Awesome users can set their kit code to unlock Font Awesome Pro icons. You can provide it through the `data-fa-kit-code` attribute or by calling the `setKitCode()` method. + +```html + + + + + +``` + ## Cherry Picking Cherry picking can be done from [the CDN](#cdn-installation-easiest) or from [npm](#npm-installation). This approach will load only the components you need up front, while limiting the number of files the browser has to download. The disadvantage is that you need to import each individual component. @@ -137,10 +151,10 @@ Cherry picking can be done from [the CDN](#cdn-installation-easiest) or from [np Here's an example that loads only the button component. Again, if you're not using a module resolver, you'll need to adjust the path to point to the folder Web Awesome is in. ```html - + - @@ -170,15 +184,15 @@ Now it's time to configure your bundler. Configurations vary for each tool, but Once your bundler is configured, you'll be able to import Web Awesome components and utilities. ```js -import '@shoelace-style/webawesome/%NPMDIR%/themes/default.css'; -import '@shoelace-style/webawesome/%NPMDIR%/components/button/button.js'; -import '@shoelace-style/webawesome/%NPMDIR%/components/icon/icon.js'; -import '@shoelace-style/webawesome/%NPMDIR%/components/input/input.js'; -import '@shoelace-style/webawesome/%NPMDIR%/components/rating/rating.js'; -import { setBasePath } from '@shoelace-style/webawesome/%NPMDIR%/utilities/base-path.js'; +import '/path/to/web-awesome/%NPMDIR%/themes/default.css'; +import '/path/to/web-awesome/%NPMDIR%/components/button/button.js'; +import '/path/to/web-awesome/%NPMDIR%/components/icon/icon.js'; +import '/path/to/web-awesome/%NPMDIR%/components/input/input.js'; +import '/path/to/web-awesome/%NPMDIR%/components/rating/rating.js'; +import { setBasePath } from '/path/to/web-awesome/%NPMDIR%/webawesome.js'; // Set the base path to the folder you copied Web Awesome's assets to -setBasePath('/path/to/webawesome/%NPMDIR%'); +setBasePath('/path/to/web-awesome/%NPMDIR%'); // , , , and are ready to use! ``` @@ -189,7 +203,7 @@ You'll notice that the CDN links all start with `/%CDNDIR%/` and npm impor TL;DR: -- `@shoelace-style/webawesome/%CDNDIR%` is for CDN users -- `@shoelace-style/webawesome/%NPMDIR%` is for npm users +- `/path/to/web-awesome/%CDNDIR%` is for CDN users +- `/path/to/web-awesome/%NPMDIR%` is for npm users This change was introduced in `v2.5.0` to address issues around installations from npm loading multiple versions of libraries (such as the Lit) that Web Awesome uses internally. diff --git a/docs/docs/resources/contributing.md b/docs/docs/resources/contributing.md index 7d2b747d3..2641209ac 100644 --- a/docs/docs/resources/contributing.md +++ b/docs/docs/resources/contributing.md @@ -201,10 +201,6 @@ This is a tip/informational callout :::warning This is a caution callout ::: - -:::danger -This is a danger callout -::: ``` #### GitHub Issues diff --git a/src/components/icon/icon.styles.ts b/src/components/icon/icon.styles.ts index 6b459f375..ee06b3b6b 100644 --- a/src/components/icon/icon.styles.ts +++ b/src/components/icon/icon.styles.ts @@ -2,16 +2,49 @@ import { css } from 'lit'; export default css` :host { + --primary-color: currentColor; + --primary-opacity: 1; + --secondary-color: currentColor; + --secondary-opacity: 0.4; + display: inline-block; - width: auto; - height: 1em; box-sizing: content-box !important; } svg { display: block; - height: 100%; - width: auto; fill: currentColor; + + .fa-primary { + color: var(--primary-color); + opacity: var(--primary-opacity); + } + + .fa-secondary { + color: var(--secondary-color); + opacity: var(--secondary-opacity); + } + } + + /* Normal width */ + :host(:not([fixed-width])) { + max-width: auto; + height: 1em; + + svg { + width: auto; + height: 1em; + } + } + + /* Fixed width */ + :host([fixed-width]) { + width: 1em; + height: 1em; + + svg { + width: 1em; + height: 1em; + } } `; diff --git a/src/components/icon/icon.ts b/src/components/icon/icon.ts index 8faf5dc08..2df51e9ef 100644 --- a/src/components/icon/icon.ts +++ b/src/components/icon/icon.ts @@ -34,6 +34,11 @@ interface IconSource { * * @csspart svg - The internal SVG element. * @csspart use - The `` element generated when using `spriteSheet: true` + * + * @cssproperty [--primary-color=currentColor] - Sets a duotone icon's primary color. + * @cssproperty [--primary-opacity=1] - Sets a duotone icon's primary opacity. + * @cssproperty [--secondary-color=currentColor] - Sets a duotone icon's secondary color. + * @cssproperty [--secondary-opacity=0.4] - Sets a duotone icon's secondary opacity. */ @customElement('wa-icon') export default class WaIcon extends WebAwesomeElement { @@ -41,6 +46,73 @@ export default class WaIcon extends WebAwesomeElement { private initialRender = false; + @state() private svg: SVGElement | HTMLTemplateResult | null = null; + + /** The name of the icon to draw. Available names depend on the icon library being used. */ + @property({ reflect: true }) name?: string; + + /** + * The family of icons to choose from. For Font Awesome Free (default), valid options include `classic` and `brands`. + * For Font Awesome Pro subscribers, valid options include, `classic`, `sharp`, `duotone`, and `brands`. Custom icon + * libraries may or may not use this property. + */ + @property({ reflect: true }) family: string; + + /** + * The name of the icon's variant. For Font Awesome, valid options include `thin`, `light`, `regular`, and `solid` for + * the `classic` and `sharp` families. Some variants require a Font Awesome Pro subscription. Custom icon libraries + * may or may not use this property. + */ + @property({ reflect: true }) variant: string; + + /** Draws the icon in a fixed-width both. */ + @property({ attribute: 'fixed-width', type: Boolean, reflect: true }) fixedWidth: false; + + /** + * An external URL of an SVG file. Be sure you trust the content you are including, as it will be executed as code and + * can result in XSS attacks. + */ + @property() src?: string; + + /** + * An alternate description to use for assistive devices. If omitted, the icon will be considered presentational and + * ignored by assistive devices. + */ + @property() label = ''; + + /** The name of a registered custom icon library. */ + @property({ reflect: true }) library = 'default'; + + connectedCallback() { + super.connectedCallback(); + watchIcon(this); + } + + firstUpdated() { + this.initialRender = true; + this.setIcon(); + } + + disconnectedCallback() { + super.disconnectedCallback(); + unwatchIcon(this); + } + + private getIconSource(): IconSource { + const library = getIconLibrary(this.library); + if (this.name && library) { + return { + url: library.resolver(this.name, this.family, this.variant), + fromLibrary: true + }; + } + + return { + url: this.src, + fromLibrary: false + }; + } + /** Given a URL, this function returns the resulting SVG element or an appropriate error symbol. */ private async resolveIcon(url: string, library?: IconLibrary): Promise { let fileData: Response; @@ -90,68 +162,6 @@ export default class WaIcon extends WebAwesomeElement { } } - @state() private svg: SVGElement | HTMLTemplateResult | null = null; - - /** The name of the icon to draw. Available names depend on the icon library being used. */ - @property({ reflect: true }) name?: string; - - /** - * The family of icons to choose from. For Font Awesome, valid options include `classic`, `sharp`, `duotone`, and - * `brands`. Custom icon libraries may or may not use this property. - */ - @property({ reflect: true }) family: string; - - /** - * The name of the icon's variant. For Font Awesome, valid options include `thin`, `light`, `regular`, and `solid` for - * the _classic_ and _sharp_ families. Custom icon libraries may or may not use this property. - */ - @property({ reflect: true }) variant: string; - - /** - * An external URL of an SVG file. Be sure you trust the content you are including, as it will be executed as code and - * can result in XSS attacks. - */ - @property() src?: string; - - /** - * An alternate description to use for assistive devices. If omitted, the icon will be considered presentational and - * ignored by assistive devices. - */ - @property() label = ''; - - /** The name of a registered custom icon library. */ - @property({ reflect: true }) library = 'default'; - - connectedCallback() { - super.connectedCallback(); - watchIcon(this); - } - - firstUpdated() { - this.initialRender = true; - this.setIcon(); - } - - disconnectedCallback() { - super.disconnectedCallback(); - unwatchIcon(this); - } - - private getIconSource(): IconSource { - const library = getIconLibrary(this.library); - if (this.name && library) { - return { - url: library.resolver(this.name, this.family, this.variant), - fromLibrary: true - }; - } - - return { - url: this.src, - fromLibrary: false - }; - } - @watch('label') handleLabelChange() { const hasLabel = typeof this.label === 'string' && this.label.length > 0; diff --git a/src/utilities/base-path.ts b/src/utilities/base-path.ts index ef5e7e8cb..94980c0cc 100644 --- a/src/utilities/base-path.ts +++ b/src/utilities/base-path.ts @@ -55,20 +55,20 @@ export function setKitCode(code: string) { * Gets the library's Web Awesome kit code. * * The kit code is used to fetch premium assets, so it needs to be set for certain components to work correctly. This - * isn't something we can infer, so the user will need to provide it using the `data-webawesome-kit` attribute. This can + * isn't something we can infer, so the user will need to provide it using the `data-fa-kit-code` attribute. This can * be on any element, but ideally it should exist on the script that imports Web Awesome. * - * + * * * Alternatively, you can set the kit code manually using the exported `setKitCode()` function. * */ export function getKitCode() { if (!kitCode) { - const el = document.querySelector('[data-webawesome-kit]'); + const el = document.querySelector('[data-fa-kit-code]'); if (el) { - setKitCode(el.getAttribute('data-webawesome-kit') || ''); + setKitCode(el.getAttribute('data-fa-kit-code') || ''); } } diff --git a/src/utilities/icon-library.ts b/src/utilities/icon-library.ts deleted file mode 100644 index ee1f84376..000000000 --- a/src/utilities/icon-library.ts +++ /dev/null @@ -1 +0,0 @@ -export { registerIconLibrary, unregisterIconLibrary } from '../components/icon/library.js'; diff --git a/src/webawesome.ts b/src/webawesome.ts index e75235c31..a1b006aa6 100644 --- a/src/webawesome.ts +++ b/src/webawesome.ts @@ -1,11 +1,10 @@ -export { getBasePath, setBasePath } from './utilities/base-path.js'; -export { registerIconLibrary, unregisterIconLibrary } from './utilities/icon-library.js'; +export { getBasePath, setBasePath, getKitCode, setKitCode } from './utilities/base-path.js'; +export { registerIconLibrary, unregisterIconLibrary } from './components/icon/library.js'; export { discover } from './utilities/autoloader.js'; // Utilities export * from './utilities/animation.js'; export * from './utilities/base-path.js'; -export * from './utilities/icon-library.js'; export * from './utilities/form.js'; // Events