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

Use requestAnimationFrame to avoid choppy experiences #1

Merged
merged 1 commit into from Aug 30, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifests/manifest-vscode.json
Expand Up @@ -3,7 +3,7 @@
"tags": [
{
"name": "image-compare",
"description": "Our ImageCompare web component class\n\nSlots:\n\n * `image-1` {} - Your first image. Will appear on the \"left\"\n\n * `image-2` {} - Your second image. Will appear on the \"right\"\n\nAttributes:\n\n * `label-text` {string} - Provide additional context to screen reader users.",
"description": "Our ImageCompare web component class\n\nSlots:\n\n * `image-1` {} - Your first image. Will appear on the \"left\"\n\n * `image-2` {} - Your second image. Will appear on the \"right\"\n\nAttributes:\n\n * `label-text` {string} - Provide additional context to screen reader users.\n\nProperties:\n\n * `animationFrame` - ",
"attributes": [
{
"name": "label-text",
Expand Down
5 changes: 5 additions & 0 deletions manifests/manifest.json
Expand Up @@ -12,6 +12,11 @@
"type": "string"
}
],
"properties": [
{
"name": "animationFrame"
}
],
"slots": [
{
"name": "image-1",
Expand Down
6 changes: 6 additions & 0 deletions manifests/manifest.md
Expand Up @@ -8,6 +8,12 @@ Our ImageCompare web component class
|--------------|----------|--------------------------------------------------|
| `label-text` | `string` | Provide additional context to screen reader users. |

## Properties

| Property |
|------------------|
| `animationFrame` |

## Slots

| Name | Description |
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Expand Up @@ -166,11 +166,17 @@ class ImageCompare extends HTMLElement {
this.attachShadow({ mode: "open" });
}

animationFrame;

connectedCallback() {
this.shadowRoot.appendChild(template.content.cloneNode(true));

this.shadowRoot.querySelector("input").addEventListener('input', ({target}) => {
this.shadowRoot.host.style.setProperty('--exposure', `${target.value}%`)
this.shadowRoot.querySelector("input").addEventListener('input', ({ target }) => {
if (this.animationFrame) cancelAnimationFrame(this.animationFrame);

requestAnimationFrame(() => {
this.shadowRoot.host.style.setProperty('--exposure', `${target.value}%`)
});
});

const customLabel = this.shadowRoot.host.getAttribute('label-text');
Expand Down