diff --git a/.github/workflows/lighthouse.yml b/.github/workflows/lighthouse.yml index dee53fa0..7cd07238 100644 --- a/.github/workflows/lighthouse.yml +++ b/.github/workflows/lighthouse.yml @@ -8,42 +8,21 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout the project - uses: actions/checkout@v2 + uses: actions/checkout@v3 - - name: Use Node.js 14.x (LTS) - uses: actions/setup-node@v1 + - name: Use Node.js 16.x (LTS) + uses: actions/setup-node@v3 with: - node-version: 14.x + node-version: 16.x + cache: 'npm' + - run: npm ci - - name: Use Ruby 2.7 - uses: actions/setup-ruby@v1 + - name: Use Ruby 2.7.3 + uses: ruby/setup-ruby@v1 with: - ruby-version: '2.7' - - - name: Restore npm cache - uses: actions/cache@v2 - with: - path: ~/.npm - key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Restore Bundler cache - uses: actions/cache@v2 - with: - path: docs/vendor/bundle - key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }} - restore-keys: | - ${{ runner.os }}-gems- - - - name: Configure Bundler cache - run: | - bundle config path vendor/bundle - - - name: Install dependencies - run: | - npm ci - cd docs && bundle install && cd .. + ruby-version: '2.7.3' + bundler-cache: true + working-directory: docs - name: Build docs run: npm run build:docs @@ -51,7 +30,7 @@ jobs: JEKYLL_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Run Lighthouse CI - run: npx @lhci/cli@0.7.x autorun + run: npx @lhci/cli@0.9.x autorun env: JEKYLL_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} LHCI_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index e907b9f3..6d84019f 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -3,8 +3,6 @@ name: Build on: pull_request: push: - branches: - - main jobs: test-node: @@ -12,20 +10,13 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout the project - uses: actions/checkout@v2 - - name: Cache node modules - uses: actions/cache@v2 + uses: actions/checkout@v3 + - name: Use Node.js 16.x (LTS) + uses: actions/setup-node@v3 with: - path: ~/.npm - key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }} - restore-keys: | - ${{ runner.os }}-node- - - name: Use Node.js 13.11.0 - uses: actions/setup-node@v1 - with: - node-version: 13.11.0 - - name: Install dependencies - run: npm i + node-version: 16.x + cache: 'npm' + - run: npm ci - name: Lint Codebase run: npm run lint - name: Run Node.js Tests diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index c3434a69..264bdd37 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -8,10 +8,12 @@ jobs: publish-npm: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - name: Checkout the project + uses: actions/checkout@v3 + - name: Use Node.js 16.x (LTS) + uses: actions/setup-node@v3 with: - node-version: 14 + node-version: 16.x registry-url: https://registry.npmjs.org/ cache: npm - run: npm ci diff --git a/.gitignore b/.gitignore index 9d37538e..732d3771 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ _site lib/ .jekyll-cache .lighthouseci +coverage diff --git a/docs/Gemfile b/docs/Gemfile index 8487316a..433346ac 100644 --- a/docs/Gemfile +++ b/docs/Gemfile @@ -2,10 +2,10 @@ source 'https://rubygems.org' -gem 'jekyll', '~> 4.1.1' +gem 'jekyll' group :jekyll_plugins do - gem 'jekyll-commonmark-ghpages', '~> 0.1.5' + gem 'jekyll-commonmark-ghpages' gem 'jekyll-github-metadata' gem 'jekyll-gzip' end diff --git a/docs/Gemfile.lock b/docs/Gemfile.lock index cae5367f..3b3e7f35 100644 --- a/docs/Gemfile.lock +++ b/docs/Gemfile.lock @@ -4,92 +4,107 @@ GEM addressable (2.8.0) public_suffix (>= 2.0.2, < 5.0) colorator (1.1.0) - commonmarker (0.17.13) - ruby-enum (~> 0.5) - concurrent-ruby (1.1.7) - em-websocket (0.5.2) + commonmarker (0.23.4) + concurrent-ruby (1.1.10) + em-websocket (0.5.3) eventmachine (>= 0.12.9) - http_parser.rb (~> 0.6.0) + http_parser.rb (~> 0) eventmachine (1.2.7) - faraday (1.0.0) + faraday (1.10.0) + faraday-em_http (~> 1.0) + faraday-em_synchrony (~> 1.0) + faraday-excon (~> 1.1) + faraday-httpclient (~> 1.0) + faraday-multipart (~> 1.0) + faraday-net_http (~> 1.0) + faraday-net_http_persistent (~> 1.0) + faraday-patron (~> 1.0) + faraday-rack (~> 1.0) + faraday-retry (~> 1.0) + ruby2_keywords (>= 0.0.4) + faraday-em_http (1.0.0) + faraday-em_synchrony (1.0.0) + faraday-excon (1.1.0) + faraday-httpclient (1.0.1) + faraday-multipart (1.0.3) multipart-post (>= 1.2, < 3) - ffi (1.13.1) + faraday-net_http (1.0.1) + faraday-net_http_persistent (1.2.0) + faraday-patron (1.0.0) + faraday-rack (1.0.0) + faraday-retry (1.0.3) + ffi (1.15.5) forwardable-extended (2.6.0) - http_parser.rb (0.6.0) - i18n (1.8.5) + http_parser.rb (0.8.0) + i18n (0.9.5) concurrent-ruby (~> 1.0) - jekyll (4.1.1) + jekyll (3.9.2) addressable (~> 2.4) colorator (~> 1.0) em-websocket (~> 0.5) - i18n (~> 1.0) - jekyll-sass-converter (~> 2.0) + i18n (~> 0.7) + jekyll-sass-converter (~> 1.0) jekyll-watch (~> 2.0) - kramdown (~> 2.1) - kramdown-parser-gfm (~> 1.0) + kramdown (>= 1.17, < 3) liquid (~> 4.0) - mercenary (~> 0.4.0) + mercenary (~> 0.3.3) pathutil (~> 0.9) - rouge (~> 3.0) + rouge (>= 1.7, < 4) safe_yaml (~> 1.0) - terminal-table (~> 1.8) - jekyll-commonmark (1.3.1) - commonmarker (~> 0.14) - jekyll (>= 3.7, < 5.0) - jekyll-commonmark-ghpages (0.1.6) - commonmarker (~> 0.17.6) - jekyll-commonmark (~> 1.2) + jekyll-commonmark (1.4.0) + commonmarker (~> 0.22) + jekyll-commonmark-ghpages (0.2.0) + commonmarker (~> 0.23.4) + jekyll (~> 3.9.0) + jekyll-commonmark (~> 1.4.0) rouge (>= 2.0, < 4.0) - jekyll-github-metadata (2.13.0) + jekyll-github-metadata (2.14.0) jekyll (>= 3.4, < 5.0) octokit (~> 4.0, != 4.4.0) - jekyll-gzip (2.4.2) + jekyll-gzip (2.5.1) jekyll (>= 3.0, < 5.0) - jekyll-sass-converter (2.1.0) - sassc (> 2.0.1, < 3.0) + jekyll-sass-converter (1.5.2) + sass (~> 3.4) jekyll-watch (2.2.1) listen (~> 3.0) - kramdown (2.3.1) + kramdown (2.4.0) rexml - kramdown-parser-gfm (1.1.0) - kramdown (~> 2.0) liquid (4.0.3) - listen (3.3.3) + listen (3.7.1) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) - mercenary (0.4.0) + mercenary (0.3.6) multipart-post (2.1.1) - octokit (4.16.0) + octokit (4.22.0) faraday (>= 0.9) sawyer (~> 0.8.0, >= 0.5.3) pathutil (0.16.2) forwardable-extended (~> 2.6) - public_suffix (4.0.6) - rb-fsevent (0.10.4) + public_suffix (4.0.7) + rb-fsevent (0.11.1) rb-inotify (0.10.1) ffi (~> 1.0) rexml (3.2.5) - rouge (3.25.0) - ruby-enum (0.7.2) - i18n + rouge (3.28.0) + ruby2_keywords (0.0.5) safe_yaml (1.0.5) - sassc (2.4.0) - ffi (~> 1.9) + sass (3.7.4) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) sawyer (0.8.2) addressable (>= 2.3.5) faraday (> 0.8, < 2.0) - terminal-table (1.8.0) - unicode-display_width (~> 1.1, >= 1.1.1) - unicode-display_width (1.7.0) PLATFORMS - ruby + x86_64-linux DEPENDENCIES - jekyll (~> 4.1.1) - jekyll-commonmark-ghpages (~> 0.1.5) + jekyll + jekyll-commonmark-ghpages jekyll-github-metadata jekyll-gzip BUNDLED WITH - 2.1.4 + 2.3.13 diff --git a/docs/_config.yml b/docs/_config.yml index 9a330ec2..6defd492 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -11,6 +11,7 @@ exclude: - Gemfile - Gemfile.lock - node_modules + - vendor collections: guide: diff --git a/docs/_guide/abilities.md b/docs/_guide/abilities.md new file mode 100644 index 00000000..c25d874c --- /dev/null +++ b/docs/_guide/abilities.md @@ -0,0 +1,52 @@ +--- +chapter: 14 +subtitle: Abilities +hidden: true +--- + +Under the hood Catalyst's controller decorator is comprised of a handful of separate "abilities". An "ability" is essentially a mixin or perhaps "higher order class". An ability takes a class and returns an extended class that adds additional behaviours. By convention all abilities exported by Catalyst are suffixed with `able` which we think is a nice way to denote that something is an ability and should be used as such. + +### Using Abilities + +Abilities are fundementally just class decorators, and so can be used just like the `@controller` decorator. For example to add only the `actionable` decorator (which automatically binds events based on `data-action` attributes): + +```typescript +import {actionable} from '@github/catalyst' + +@actionable +class HelloWorld extends HTMLElement { +} +``` + +### Using Marks + +Abilities also come with complementary field decorators which we call "marks" (we give them a distinctive name because they're a more restrictive subset of field decorators). Marks annotate fields which abilities can then extend with custom logic, both [Targets]({{ site.baseurl }}/guide/targets) and [Attrs]({{ site.baseurl }}/guide/attrs) are abilities that use marks. The `targetable` ability includes `target` & `targets` marks, and the `attrable` ability includes the `attr` mark. Marks decorate individual fields, like so: + +```typescript +import {targetable, target, targets} from '@github/catalyst' + +@targetable +class HelloWorldElement extends HTMLElement { + @target name + @targets people +} +``` + +Marks _can_ decorate over fields, get/set functions, or class methods - but individual marks can set their own validation logic, for example enforcing a naming pattern or disallowing application on methods. + +### Built-In Abilities + +Catalyst ships with a set of built in abilities. The `@controller` decorator applies the following built-in abilities: + +- `controllable` - the base ability which other abilities require for functionality. +- `targetable` - the ability to define `@target` and `@targets` properties. See [Targets]({{ site.baseurl }}/guide/targets) for more. +- `actionable` - the ability to automatically bind events based on `data-action` attributes. See [Actions]({{ site.baseurl }}/guide/actions) for more. +- `attrable` - the ability to define `@attr`s. See [Attrs]({{ site.baseurl }}/guide/attrs) for more. + +The `@controller` decorator also applies the `@register` decorator which automatically registers the element in the Custom Element registry, however this decorator isn't an "ability". + +The following abilities are shipped with Catalyst but require manually applying as they aren't considered critical functionality: + + - `providable` - the ability to define `provider` and `consumer` properties. See [Providable]({{ site.baseurl }}/guide/providable) for more. + +In addition to the provided abilities, Catalyst provides all of the tooling to create your own custom abilities. Take a look at the [Create Ability]({{ site.baseurl }}/guide/create-ability) documentation for more! diff --git a/docs/_guide/create-ability.md b/docs/_guide/create-ability.md new file mode 100644 index 00000000..99fa309f --- /dev/null +++ b/docs/_guide/create-ability.md @@ -0,0 +1,229 @@ +--- +chapter: 16 +subtitle: Create your own abilities +hidden: true +--- + +Catalyst provides the functionality to create your own abilities, with a few helper methods and a `controllable` base-level ability. These are explained in detail below, but for a quick summary they are: + + - `createAbility` - a helper function to make new abilities (class decorators). + - `createMark` - a helper function to generate class field & method decorators. + - `tag-observer` - a set of helper functions to watch for tagged children in an element's subtree. + - `controllable` - the base ability which allows interacting with semi-private parts of an element. + +## createAbility + +This function allows you to make your own [Ability]({{ site.baseurl }}/guide/abilities). Abilities are really Class Decorators, but there's a couple of things that `createAbility` provides to simplify the ergonomics of Class Decorators: + + - TypeScript can be a little tricky when working with Class Decorators. `createAbility` simplifies this a bit. + - JavaScript does not copy over the `name` property when extending a class (e.g. via a decorator), and it can be a little cumbersome to do this, so `createAbility` does this for you. + - Abilities are [idempotent](https://en.wikipedia.org/wiki/Idempotence). Class decorators are not idempotent by default, which means applying a decorator multiple times can cause issues. `createAbility` mitigates this by memoizing the classes it has applied to, meaning applying an ability multiple times has no effect past the first application. + +The above three features of `createAbility` make it really useful when creating mixins for web components, and makes them much easier for developers as they can trust an ability to not be sensitive to these problems. + +To create an ability, call the `createAbility` method and pass in a callback function which takes a `CustomElementClass` and returns a new class. You can also provide extra types if your returned class adds new methods or fields. Here's an example, using TypeScript: + + +```typescript +import type {CustomElementClass} from '@github/catalyst' +import {createAbility} from '@github/catalyst' + +// by convention, abilities end in "able" +interface Fooable { + foo(): void // This interface has additional methods on top of `CustomElementClass`! +} + +// Fooable: automatically calls `foo()` on `connectedCallback` +export const fooable = createAbility( + // ↓ Notice the `& { new (): Fooable }` + (Class: T): T & { new (): Fooable } => + class extends Class { + connectedCallback() { + this.foo() + } + + foo() { + console.log('Foo was called!') + } + } +) +``` + +Inside the `class extends Class` block, you can author custom element logic that you might want to make reusable across a multitude of elements. You can also adjust the input type to subclass `CustomElementClass`, which can be useful for setting up a contract between your Ability and the classes that rely on it: + +```typescript +import type {CustomElementClass} from '@github/catalyst' +import {createAbility} from '@github/catalyst' + +// by convention, abilities end in "able" +interface Fooable { + foo(): void // This interface has additional methods on top of `CustomElementClass`! +} + +interface FooableClass { + new(...args: any[]): Fooable +} + +// Fooable: automatically calls `foo()` on `connectedCallback` +export const fooable = createAbility( + // ↓ Notice the `& FooableClass` + (Class: T): T => + class extends Class { + // TypeScript will expect the constructor to be defined for a mixin + constructor(...args: any[]) { + super(...args) + } + + connectedCallback() { + // Classes that apply this ability _must_ implement `foo()`. + super.foo() + } + } +``` + +If you're interested in some advanced examples, you can take a look at the Catalyst source code - every feature of Catalyst is an Ability! + +## createMark + +This function allows you to make annotations for fields (like `@attr` and `@target`). Marks are really Field/Method Decorators, but with simpler ergonomics: + + - Marks are only initialized on instances, which makes them easier to reason about. + - Marks are not configurable, which keeps them simple. + - They are built to ease a transition between TypeScript decorators and ECMAScript decorators, which will help as decorators become standardised. + +`createMark` can be called with a `validate` and an `init` function, and gives back a tuple of 3 functions: the decorator itself, a function to get a list of marks that an instance has, and a function that will initialise the marks on an instance. It can be used like so: + +```typescript + +// Makes the @prop decorator +const [prop, getProps, initProps] = createMark( + ({name, kind}) => { + // Validate the name and kind that a mark can have. + // Name will be the PropertyKey that was decorated, and `kind` will be one of: + // "method", "field", "getter", "setter". + if (kind === "method") { + throw new Error(`@prop cannot be used to mark a method`) + } + }, + (instance: CustomElement, {name, kind, access}) => { + // Put field initialization logic here. + // Return a property descriptor to define a field's functionality: + let value = kind === 'field' ? access.value : access.get?.call(instance) + return { + get() { return value } + set(newValue) { + value = newValue + instance.propChanged(name, newValue) + } + } + } +) +``` + +If you want to find some examples of how marks work, take a look at the Catalyst source code! All field decorators (`@attr`, `@target`, `@provide`, `@consume` and so on) use `createMark`. + +## tag-observer + +Tag Observer provides a set of functions to observe elements marked with well-known attributes across the DOM, allowing classes to be reactive to DOM mutations. These functions operate over a `MutationObserver` set up to detect new elements coming into the page that have a registered attribute. To call register a new tag you can use the `registerTag` function which takes an attribute name to observe, a parse function (that parses the attribute value), and a found function (which is called for each element that has the attribute): + +```typescript +registerTag( + `data-foo`, + (value: string) => value.split('.'), + (el: Element, controller: Element | ShadowRoot, ...meta: string[]) => { + // ... + } +) +``` + +Tag Observer also provides a `observeElementForTags` function, which can be called on an element to adopt it into observation. A good place to use this is in your Abilities `connectedCallback`. This function can also take a `shadowRoot` if you're interested in observing tags within the shadow DOM (recommended). This function will find the root element (`ownerDocument`) and begin observing it. + +```typescript +export const fooable = createAbility( + (Class: T): T => + class extends Class { + connectedCallback() { + observeElementForTags(this) // This elements ownerDocument will now look out for new tags + } + } +``` + +Whenever an element appears on the page with the matching attribute (e.g. `data-foo`), the value is extracted, split by whitespace, and each substring is then given to `parse` to turn into an array of strings. The first value in the array that the parse function returns must be a parent selector, which is then used to find the controller this attribute could pertain to. If the element is a child of the given controller selector, then the found function is called with the element, the controller, and any additional metadata that the parse function extracted. Let's see an example for how this might work, given the above registered tag: + +```html + +
+
+``` + +- Our `data-foo` attribute is found in the DOM, belonging to the `div` element. +- The value is extracted and split by whitespace. +- Our parse function gets called twice, firstly with `my-element.foo.bar` + - The parse function splits this by `.` which gets us `['my-element', 'foo', 'bar']`. + - Tag observer uses `my-element` as the parent selector and calls `div.closest('my-element')`, + - The `` controller is found. + - Our found function is called with `(
, , ['foo', 'bar'])` +- The parse function is also called with `other-element.baz.bing`. + - The parse function splits this by `.` which gets us `['other-element', 'baz', 'bing']`. + - Tag observer uses `other-element` as the parent selector and calls `div.closest('other-element')`, + - No parent element is found, so the found function is not called. + +To take a look at how Tag Observer is used in Catalyst, you can look at [`data-action` (the Actionable ability)]({{ site.baseurl }}/guide/actions) or [`data-target` & `data-targets` (the Targetable ability)]({{ site.baseurl }}/guide/targets). + +## controllable + +`controllable` is a basic ability which other abilities can use to simplify connecting to a custom elements private state. This ability isn't _required_ to be used when creating your own abilities, but it's very useful for abilities which expect to use either the [ShadowDOM](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot) or [ElementInternals](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals). + +You can create an ability that itself uses the `controllable` ability like so: + +```typescript +import type {CustomElementClass} from '@github/catalyst' +import {createAbility, controllable} from '@github/catalyst' + +createAbility((Class: CustomElementClass) => class extends controllable(Class) { + // Your behaviour goes here! +} +``` + +The `controllable` ability provides 2 _custom_ callbacks which allow you to safely & robustly intercept the attachment of a ShadowRoot, and the attachment of ElementInternals. Let's look at each: + +### `[attachShadowCallback](shadowRoot: ShadowRoot)` + +```typescript +import type {CustomElementClass} from '@github/catalyst' +import {createAbility, attachShadowCallback, controllable} from '@github/catalyst' + +createAbility((Class: CustomElementClass) => class extends controllable(Class) { + [attachShadowCallback](shadowRoot: ShadowRoot) { + super[attachShadowCallback](shadowRoot) + // Do stuff with the `shadowRoot`. + } +} +``` + +`attachShadowCallback` is a special `Symbol()` which allows you to make a method mostly hidden from other classes. `controllable` will call this symbol method whenever a ShadowRoot is attached to the element, which can be attached in 2 different ways: + + - During the constructor, where the element might recieve a declarative ShadowDOM root (closed or open). + - Any time the `attachShadow()` function is called. + +This method is _usually_ called zero or once, but may be called twice if the element recieves a Declarative ShadowDOM root, and overrides this with another call to `attachShadow()`. + +Simply overriding `this.attachShadow` or trying to access `this.shadowRoot` can be a little tricky (if an element has a closed declarative shadow root this can be especially difficult to access within mixins), so this callback can be especially useful for working around the various ways a shadowRoot can be created. + +### `[attachInternalsCallback](internals: ElementInternals)` + +```typescript +import type {CustomElementClass} from '@github/catalyst' +import {createAbility, attachInternalsCallback, controllable} from '@github/catalyst' + +createAbility((Class: CustomElementClass) => class extends controllable(Class) { + [attachInternalsCallback](internals: ElementInternals) { + super[attachInternalsCallback](internals) + // Do stuff with the `internals`. + } +} +``` + +`attachInternalsCallback` is a special `Symbol()` which allows you to make a method mostly hidden from other classes. `controllable` will call this symbol method whenever an element is constructed, giving it the element's `ElementInternals`. This enables custom enablies [Abilities]({{ site.baseurl }}/guide/abilities) to also have access to `ElementInternals`. It does so while also preserving the ability for `attachInternals()` to be called again (usually `attachInternals()` will error if called twice). + +If you need access to the internals, then the `attachInternalsCallback` can be very useful as it protects you from calling `attachInternals` in a way which the concrete classes will then fail. diff --git a/docs/_guide/decorators.md b/docs/_guide/decorators.md index 806e30fa..10d08343 100644 --- a/docs/_guide/decorators.md +++ b/docs/_guide/decorators.md @@ -37,7 +37,51 @@ class HelloWorldElement extends HTMLElement { Class Field decorators get given the class and the field name so they can add custom functionality to the field. Because they operate on the fields, they must be put on top of or to the left of the field. -#### Supporting `strictPropertyInitialization` +### Method Decorators + +Method decorators work just like Field Decorators. Put them on top or on the left of the method, like so: + + +```js +class HelloWorldElement extends HTMLElement { + + @log + submit() { + // ... + } + + // Alternative style + + @log load() { + // ... + } + +} +``` + +### Getter/Setter + +Decorators can also be used over a `get` or `set` field. These work just like Field Decorators, but you can put them over one or both the `get` or `set` field. Some decorators might throw an error if you put them over a `get` field, when they expect to be put over a `set` field: + + +```js +class HelloWorldElement extends HTMLElement { + + @target set something() { + // ... + } + + // Can be used over just one field + @attr get data() { + return {} + } + set data() { + + } +} +``` + +### Supporting `strictPropertyInitialization` TypeScript comes with various "strict" mode settings, one of which is `strictPropertyInitialization` which lets TypeScript catch potential class properties which might not be assigned during construction of a class. This option conflicts with Catalyst's `@target`/`@targets` decorators, which safely do the assignment but TypeScript's simple heuristics cannot detect this. There are two ways to work around this: @@ -63,28 +107,6 @@ TypeScript comes with various "strict" mode settings, one of which is `strictPro } ``` -### Method Decorators - -Catalyst doesn't currently ship with any method decorators, but you might see them in code. They work just like Field Decorators (in fact they're the same thing). Put them on top or on the left of the method, like so: - - -```js -class HelloWorldElement extends HTMLElement { - - @log - submit() { - // ... - } - - // Alternative style - - @log load() { - // ... - } - -} -``` - ### Function Calling Decorators You might see some decorators that look like function calls, and that's because they are! Some decorators allow for customisation; calling with additional arguments. Decorators that expect to be called are generally not interchangeable with the non-call variant, a decorators documentation should tell you how to use it. diff --git a/docs/_guide/providable.md b/docs/_guide/providable.md new file mode 100644 index 00000000..bbc48de4 --- /dev/null +++ b/docs/_guide/providable.md @@ -0,0 +1,145 @@ +--- +chapter: 15 +subtitle: The Provider pattern +hidden: true +--- + +The [Provider pattern](https://www.patterns.dev/posts/provider-pattern/) allows for deeply nested children to ask ancestors for values. This can be useful for decoupling state inside a component, centralising it higher up in the DOM heirarchy. A top level container component might store values, and many children can consume those values, without having logic duplicated across the app. It's quite an abstract pattern so is better explained with examples... + +Say for example a set of your components are built to perform actions on a user, but need a User ID. One way to handle this is to set the User ID as an attribute on each element, but this can lead to a lot of duplication. Instead these actions can request the ID from a parent component, which can provide the User ID without creating an explicit relationship (which can lead to brittle code). + +The `@providable` ability allows a Catalyst controller to become a provider or consumer (or both) of one or many properties. To provide a property to nested controllers that ask for it, mark a property as `@provide`. To consume a property from a parent, mark a property as `@consume`. Let's try implementing the user actions using `@providable`: + +```typescript +import {providable, consume, provide, controller} from '@github/catalyst' + +@controller +@providable +class BlockUser extends HTMLElement { + // This will request `userId`, and default to '' if not provided. + @consume userId = '' + // This will request `userName`, and default to '' if not provided. + @consume userName = '' + + async handleEvent() { + if (confirm(`Would you like to block ${this.userName}?`)) { + await fetch(`/users/${userId}/delete`) + } + } +} + +@controller +@providable +class FollowUser extends HTMLElement { + // This will request `userId`, and default to '' if not provided. + @consume userId = '' + // This will request `userName`, and default to '' if not provided. + @consume userName = '' + + async handleEvent() { + if (confirm(`Would you like to follow ${this.userName}?`)) { + await fetch(`/users/${userId}/delete`) + } + } +} + +@controller +@providable +class UserRow extends HTMLElement { + // This will provide `userId` as '123' to any nested children that request it. + @provide userId = '123' + // This will provide `userName` as 'Alex' to any nested children that request it. + @provide userName = 'Alex' +} +``` + +```html + +
+ + ` + // We need to wait for one microtask after injecting the HTML into to + // controller so that the actions have been bound to the controller. await Promise.resolve() - button.click() + + instance.querySelector('button')!.click() expect(instance.foo).to.have.callCount(1) }) - }) - it('re-binds actions deeply in the HTML', async () => { - instance = await fixture(html``) - bind(instance) - listenForBind(instance.ownerDocument) - instance.innerHTML = ` -
-
-
-
- ` - // We need to wait for one microtask after injecting the HTML into to - // controller so that the actions have been bound to the controller. - await Promise.resolve() - instance.querySelector('button').click() - expect(instance.foo).to.have.callCount(1) - }) + it('will not fire if the binding attribute is removed', () => { + expect(instance.foo).to.have.callCount(0) + const el = instance.querySelector('#el1')! - it('will not fire if the binding attribute is removed', async () => { - instance = await fixture(html` -
-
`) - bind(instance) - expect(instance.foo).to.have.callCount(0) - const el = instance.querySelector('div') - el.click() - expect(instance.foo).to.have.callCount(1) - el.setAttribute('data-action', 'click:other-element#foo') - el.click() - expect(instance.foo).to.have.callCount(1) - }) + el.click() + expect(instance.foo).to.have.callCount(1) + el.setAttribute('data-action', 'click:other-element#foo') + el.click() + expect(instance.foo).to.have.callCount(1) + }) - it('will rebind elements if the attribute changes', async () => { - instance = await fixture(html` - - `) - bind(instance) - listenForBind(instance.ownerDocument) - await Promise.resolve() - const button = instance.querySelector('button') - button.click() - expect(instance.foo).to.have.callCount(0) - button.setAttribute('data-action', 'click:bind-test-element#foo') - await Promise.resolve() - button.click() - expect(instance.foo).to.have.callCount(1) + it('will rebind elements if the attribute changes', async function () { + expect(instance.foo).to.have.callCount(0) + const el = instance.querySelector('#el1')! + + el.click() + expect(instance.foo).to.have.callCount(1) + el.setAttribute('data-action', 'submit:bind-test#foo') + el.click() + expect(instance.foo).to.have.callCount(1) + + // We need to wait for one microtask after injecting the HTML into to + // controller so that the actions have been bound to the controller. + await Promise.resolve() + + el.dispatchEvent(new CustomEvent('submit')) + expect(instance.foo).to.have.callCount(2) + }) }) }) diff --git a/test/controllable.ts b/test/controllable.ts new file mode 100644 index 00000000..812819d9 --- /dev/null +++ b/test/controllable.ts @@ -0,0 +1,183 @@ +import type {CustomElementClass, CustomElement} from '../src/custom-element.js' +import {expect, fixture, html} from '@open-wc/testing' +import {fake} from 'sinon' +import {controllable, attachShadowCallback, attachInternalsCallback} from '../src/controllable.js' + +describe('controllable', () => { + describe('attachShadowCallback', () => { + let attachShadowFake: (shadow: ShadowRoot) => void + let shadow: ShadowRoot | null + beforeEach(() => { + shadow = null + attachShadowFake = fake() + }) + + const attachShadow = (Class: T): T => + class extends controllable(Class) { + [attachShadowCallback](...args: [ShadowRoot]) { + super[attachShadowCallback]?.(...args) + return attachShadowFake.apply(this, args) + } + } + + @attachShadow + @controllable + class DeclarativeShadowAbility extends HTMLElement { + constructor() { + super() + // Declarative shadows run before constructor() is available, but + // abilities run after element constructor + shadow = HTMLElement.prototype.attachShadow.call(this, {mode: 'closed'}) + } + [attachShadowCallback]() { + throw new Error('Custom Element concrete class [attachShadowCallback] should not have been called') + } + } + customElements.define('declarative-shadow-ability', DeclarativeShadowAbility) + + @attachShadow + @controllable + class ClosedShadowAbility extends HTMLElement { + constructor() { + super() + shadow = this.attachShadow({mode: 'closed'}) + } + [attachShadowCallback]() { + throw new Error('Custom Element concrete class [attachShadowCallback] should not have been called') + } + } + customElements.define('closed-shadow-ability', ClosedShadowAbility) + + @attachShadow + @controllable + class ConnectedShadowAbility extends HTMLElement { + connectedCallback() { + shadow = this.attachShadow({mode: 'closed'}) + } + [attachShadowCallback]() { + throw new Error('Custom Element concrete class [attachShadowCallback] should not have been called') + } + } + customElements.define('connected-shadow-ability', ConnectedShadowAbility) + + @attachShadow + @controllable + class ManualShadowAbility extends HTMLElement { + [attachShadowCallback]() { + throw new Error('Custom Element concrete class [attachShadowCallback] should not have been called') + } + } + customElements.define('manual-shadow-ability', ManualShadowAbility) + + @attachShadow + @controllable + class DisallowedShadowAbility extends HTMLElement { + static disabledFeatures = ['shadow'] + } + customElements.define('disallowed-shadow-ability', DisallowedShadowAbility) + + it('is called with shadowRoot of declarative ShadowDOM', async () => { + const instance = await fixture(html``) + expect(shadow).to.exist.and.be.instanceof(ShadowRoot) + expect(attachShadowFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(shadow) + }) + + it('is called with shadowRoot from attachShadow call', async () => { + const instance = await fixture(html``) + shadow = instance.attachShadow({mode: 'closed'}) + expect(shadow).to.exist.and.be.instanceof(ShadowRoot) + expect(attachShadowFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(shadow) + }) + + it('is called with shadowRoot from attachInternals call', async () => { + const instance = await fixture(html``) + expect(shadow).to.exist.and.be.instanceof(ShadowRoot) + expect(attachShadowFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(shadow) + }) + + it('is called with shadowRoot from connectedCallback', async () => { + const instance = await fixture(html``) + expect(shadow).to.exist.and.be.instanceof(ShadowRoot) + expect(attachShadowFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(shadow) + }) + + it('does not error if shadowdom is disabled', async () => { + await fixture(html``) + expect(attachShadowFake).to.be.have.callCount(0) + }) + }) + + describe('attachInternalsCallback', () => { + let attachInternalsFake: (internals: ElementInternals) => void + let internals: ElementInternals | null + beforeEach(() => { + internals = null + attachInternalsFake = fake() + }) + + const attachInternals = (Class: T): T => + class extends controllable(Class) { + [attachInternalsCallback](...args: [ElementInternals]) { + super[attachInternalsCallback]?.(...args) + return attachInternalsFake.apply(this, args) + } + } + + @attachInternals + @controllable + class InternalsAbility extends HTMLElement { + constructor() { + super() + internals = this.attachInternals() + } + [attachInternalsCallback]() { + throw new Error('Custom Element concrete class [attachInternalsCallback] should not have been called') + } + } + customElements.define('internals-ability', InternalsAbility) + + @attachInternals + @controllable + class ManualInternalsAbility extends HTMLElement { + [attachInternalsCallback]() { + throw new Error('Custom Element concrete class [attachInternalsCallback] should not have been called') + } + } + customElements.define('manual-internals-ability', ManualInternalsAbility) + + @attachInternals + @controllable + class DisallowedInternalsAbility extends HTMLElement { + static disabledFeatures = ['internals']; + [attachInternalsCallback]() { + throw new Error('Custom Element concrete class [attachInternalsCallback] should not have been called') + } + } + customElements.define('disallowed-internals-ability', DisallowedInternalsAbility) + + it('is called on constructor', async () => { + const instance = await fixture(html``) + expect(attachInternalsFake).to.be.calledOnce.calledOn(instance) + }) + + it('does not prevent attachInternals being called by userland class', async () => { + const instance = await fixture(html``) + expect(internals).to.exist.and.be.instanceof(ElementInternals) + expect(attachInternalsFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(internals) + }) + + it('errors if userland calls attachInternals more than once', async () => { + const instance = await fixture(html``) + internals = instance.attachInternals() + expect(internals).to.exist.and.be.instanceof(ElementInternals) + expect(attachInternalsFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(internals) + + expect(() => instance.attachInternals()).to.throw(DOMException) + }) + + it('does not error if element internals are disabled', async () => { + await fixture(html``) + expect(attachInternalsFake).to.have.callCount(0) + }) + }) +}) diff --git a/test/controller.ts b/test/controller.ts index 232fede0..cb6abe57 100644 --- a/test/controller.ts +++ b/test/controller.ts @@ -25,7 +25,6 @@ describe('controller', () => { it('binds controllers before custom connectedCallback behaviour', async () => { @controller - // eslint-disable-next-line @typescript-eslint/no-unused-vars class ControllerBindOrderElement extends HTMLElement { foo = fake() } @@ -36,7 +35,7 @@ describe('controller', () => { this.dispatchEvent(new CustomEvent('loaded')) } } - instance = await fixture(html` + instance = await fixture(html` @@ -46,36 +45,34 @@ describe('controller', () => { it('binds shadowRoots after connectedCallback behaviour', async () => { @controller - // eslint-disable-next-line @typescript-eslint/no-unused-vars class ControllerBindShadowElement extends HTMLElement { connectedCallback() { this.attachShadow({mode: 'open'}) const button = document.createElement('button') button.setAttribute('data-action', 'click:controller-bind-shadow#foo') - this.shadowRoot.appendChild(button) + this.shadowRoot!.appendChild(button) } foo() { return 'foo' } } - instance = await fixture(html``) + instance = await fixture(html``) replace(instance, 'foo', fake(instance.foo)) - instance.shadowRoot.querySelector('button').click() + instance.shadowRoot!.querySelector('button')!.click() expect(instance.foo).to.have.callCount(1) }) it('binds auto shadowRoots', async () => { @controller - // eslint-disable-next-line @typescript-eslint/no-unused-vars class ControllerBindAutoShadowElement extends HTMLElement { foo() { return 'foo' } } - instance = await fixture(html` + instance = await fixture(html`