Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { h, cloneElement, render, hydrate } from 'preact';
* @typedef {import('./index.d.ts').PreactCustomElement} PreactCustomElement
*/


/**
* @type {import('./index.d.ts').default}
*/
Expand Down Expand Up @@ -48,15 +47,16 @@ export default function register(Component, tagName, propNames, options) {
propNames.forEach((name) => {
Object.defineProperty(PreactElement.prototype, name, {
get() {
return this._vdom.props[name];
return this._vdom
? this._vdom.props[name]
: this._props[name];
},
set(v) {
if (this._vdom) {
this.attributeChangedCallback(name, null, v);
} else {
if (!this._props) this._props = {};
this._props[name] = v;
this.connectedCallback();
}

// Reflect property changes to attributes if the value is a primitive
Expand Down
30 changes: 28 additions & 2 deletions src/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@ describe('web components', () => {
});
assert.equal(other, 1);
});

it('sets complex property after other property', () => {
const el = document.createElement('x-dummy-button');

// set simple property first
el.text = 'click me';

let clicks = 0;
const onClick = () => clicks++;
el.onClick = onClick;

assert.equal(el.text, 'click me');
assert.equal(el.onClick, onClick);

root.appendChild(el);
assert.equal(
root.innerHTML,
'<x-dummy-button text="click me"><button>click me</button></x-dummy-button>'
);

act(() => {
el.querySelector('button').click();
});

assert.equal(el.onClick, onClick);
assert.equal(clicks, 1);
});
});

function Foo({ text, children }) {
Expand Down Expand Up @@ -327,8 +354,7 @@ describe('web components', () => {

const child = document
.querySelector('x-adopted-style-sheets')
.shadowRoot
.querySelector('.styled-child');
.shadowRoot.querySelector('.styled-child');

const style = getComputedStyle(child);
assert.equal(style.color, 'rgb(255, 0, 0)');
Expand Down