Skip to content

Commit

Permalink
Merge branch 'main' into node-16-in-codespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Apr 30, 2022
2 parents b6b7b17 + 91529fc commit 395bf7b
Show file tree
Hide file tree
Showing 27 changed files with 10,451 additions and 8,631 deletions.
8 changes: 6 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"plugins": ["github"],
"extends": ["plugin:github/recommended", "plugin:github/typescript", "plugin:github/browser"],
"rules": {
"no-invalid-this": "off",
"@typescript-eslint/no-invalid-this": ["error"],
"import/no-unresolved": "off",
"github/no-inner-html": "off",
"i18n-text/no-en": "off",
"import/extensions": ["error", "ignorePackages"],
"@typescript-eslint/consistent-type-imports": ["error", {"prefer": "type-imports"}]
},
Expand All @@ -24,6 +25,9 @@
},
{
"files": "*.cjs",
"rules": {
"import/no-commonjs": "off"
},
"env": {
"node": true
}
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ jobs:
node-version: 13.11.0
- name: Install dependencies
run: npm i
- name: Lint Codebase
run: npm run lint
- name: Run Node.js Tests
run: npm run test
- name: Check Bundle Size
run: npm run size
24 changes: 24 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Publish

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
registry-url: https://registry.npmjs.org/
cache: npm
- run: npm ci
- run: npm test
- run: npm version ${TAG_NAME} --git-tag-version=false
env:
TAG_NAME: ${{ github.event.release.tag_name }}
- run: npm whoami; npm --ignore-scripts publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @github/ui-frameworks-reviewers
* @github/web-systems-reviewers
57 changes: 57 additions & 0 deletions docs/_guide/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ class FuzzySearchElement extends HTMLElement {
}
```

Alternatively, if you'd like more precise control over the exact way debouncing happens (for example you'd like to make the debounce timeout dynamic, or sometimes call _without_ debouncing), you can have two methods following the pattern of `foo`/`fooNow` or `foo`/`fooSync`, where the non-suffixed method dispatches asynchronously to the `Now`/`Sync` suffixed method, a little like this:

```typescript
import {controller} from '@github/catalyst'

@controller
class FuzzySearchElement extends HTMLElement {

#searchAnimationFrame = 0
search(event: Event) {
clearAnimationFrame(this.#searchAnimationFrame)
this.#searchAnimationFrame = requestAnimationFrame(() => this.searchNow(event: Event))
}

searchNow(event: Event) {
const value = event.currentTarget.value
// This function is very computationally intensive, so we should run it as little as possible
this.filterAllItemsWithValue(value)
}

}
```

### Aborting Network Requests

When making network requests using `fetch`, based on user input, you can cancel old requests as new ones come in. This is useful for performance as well as UI responsiveness, as old requests that aren't cancelled might complete later than newer ones, and causing the UI to jump around. Aborting network requests requires you to use [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) (a web platform feature).
Expand Down Expand Up @@ -70,3 +93,37 @@ class RemoveSearchElement extends HTMLElement {
}
}
```

### Registering global or many event listeners

Generally speaking, you'll want to use ["Actions"]({{ site.baseurl }}/guide/actions) to register event listeners with your Controller, but Actions only work for components nested within your Controller. It may also be necessary to listen for events on the Document, Window, or across well-known adjacent elements. We can manually call `addEventListener` for these types, including during the `connectedCallback` phase. Cleanup for `addEventListener` can be a bit error prone, but [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) can be useful here to pass a signal that the element is cleaning up. AbortControllers should be created once per `connectedCallback`, as they are not re-usable, while Controllers _can_ be reused.


```typescript
@controller
class UnsavedChangesElement extends HTMLElement {

#eventAbortController: AbortController|null = null

connectedCallback(event: Event) {
// Create the new AbortController and get the new signal
const {signal} = (this.#eventAbortController = new AbortController())

// You can `signal` as an option to any `addEventListener` call:
window.addEventListener('hashchange', this, { signal })
window.addEventListener('blur', this, { signal })
window.addEventListener('popstate', this, { signal })
window.addEventListener('pagehide', this, { signal })
}

disconnectedCallback() {
// This will clean up any `addEventListener` calls which were given the `signal`
this.#eventAbortController?.abort()
}

handleEvent(event) {
// `handleEvent` will be called when each one of the event listeners
// defined in `connectedCallback` is dispatched.
}
}
```
2 changes: 1 addition & 1 deletion docs/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function storeColorSchemePreference() {
// Get color scheme preference from URL
const url = new URL(window.location.href)
const url = new URL(window.location.href, window.location.origin)
const params = new URLSearchParams(url.search)

// Return early if there’s nothing to store
Expand Down
Loading

0 comments on commit 395bf7b

Please sign in to comment.