Skip to content

Commit

Permalink
Merge pull request #142 from matthewp/attr
Browse files Browse the repository at this point in the history
Allow the "title" attribute to be reflected
  • Loading branch information
matthewp committed Sep 27, 2019
2 parents 1680cbc + 8c00852 commit 26e2000
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -7,7 +7,7 @@ all: haunted.js web.js
lib/*.js: src/*.ts
$(TRANSPILE)
# Add ".js" extension to module imports
gsed -i -E "s/from '.\/(.*?)'/from '.\/\1.js'/" lib/*.js
sed -i -E "s/from '.\/(.*?)'/from '.\/\1.js'/" lib/*.js

haunted.js: lib/*.js
$(COMPILE) -f es -o $@ -e lit-html lib/haunted.js
Expand Down
14 changes: 12 additions & 2 deletions src/component.ts
Expand Up @@ -74,7 +74,10 @@ function makeComponent(render: RenderFunction): Creator {
this._scheduler.teardown();
}

attributeChangedCallback(name: string, _: unknown, newValue: unknown): void {
attributeChangedCallback(name: string, oldValue: unknown, newValue: unknown): void {
if(oldValue === newValue) {
return;
}
let val = newValue === '' ? true : newValue;
Reflect.set(this, toCamelCase(name), val);
}
Expand All @@ -101,10 +104,17 @@ function makeComponent(render: RenderFunction): Creator {
},

set(target, key: string, value, receiver): boolean {
let desc: PropertyDescriptor | undefined;
if(key in target) {
desc = Object.getOwnPropertyDescriptor(target, key);
if(desc && desc.set) {
desc.set.call(receiver, value);
return true;
}

Reflect.set(target, key, value);
}
let desc: PropertyDescriptor;

if(typeof key === 'symbol' || key[0] === '_') {
desc = {
enumerable: true,
Expand Down
19 changes: 19 additions & 0 deletions test/test-attrs.js
Expand Up @@ -26,6 +26,25 @@ describe('Observed attributes', () => {
teardown();
});

it('Can observe the "title" attribute', async () => {
const tag = 'attrs-test-title';

function app() {
this.setAttribute('title', 'bar');
}

app.observedAttributes = ['title'];

customElements.define(tag, component(app));

let teardown = attach(tag);
await cycle();

let inst = document.querySelector(tag);
assert.equal(inst.getAttribute('title'), 'bar', 'on the instance');
teardown();
});

it('Trigger rerenders while declared as an option', async () => {
const tag = 'attrs-options-test';

Expand Down

0 comments on commit 26e2000

Please sign in to comment.