Skip to content

Commit

Permalink
Refactored defined constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed Nov 13, 2023
1 parent b9aee43 commit 0bb72a6
Show file tree
Hide file tree
Showing 17 changed files with 901 additions and 919 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-comics-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/dna': minor
---

The `define` method now returns the decorated constructor.
5 changes: 5 additions & 0 deletions .changeset/stale-dryers-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/dna': minor
---

Remove `customElementPrototype` decorator.
114 changes: 60 additions & 54 deletions docs/guide/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@ class Button extends builtin.HTMLButtonElement {
```ts [get listeners]
import { builtin, define } from '@chialab/dna';

class Button extends builtin.HTMLButtonElement {
static get listeners() {
return {
'click': function (event) {
event.preventDefault();
},
'input [name="age"]': function (event, target) {
console.log(target.value);
},
};
const Button = define(
'x-button',
class extends builtin.HTMLButtonElement {
static get listeners() {
return {
'click': function (event) {
event.preventDefault();
},
'input [name="age"]': function (event, target) {
console.log(target.value);
},
};
}
},
{
extends: 'button',
}
}

define('x-button', Button, {
extends: 'button',
});
);
```

:::
Expand All @@ -70,20 +72,21 @@ class Tracker extends Component {
```ts [get listeners]
import { Component, define } from '@chialab/dna';

class Tracker extends Component {
static get listeners() {
return {
touchmove: {
callback(event) {
// ...
const Tracker = define(
'x-tracker',
class extends Component {
static get listeners() {
return {
touchmove: {
callback(event) {
// ...
},
passive: true,
},
passive: true,
},
};
};
}
}
}

define('x-tracker', Tracker);
);
```

:::
Expand Down Expand Up @@ -133,21 +136,22 @@ class Tracker extends Component {
```ts [get listeners]
import { Component, define } from '@chialab/dna';

class Tracker extends Component {
static get listeners() {
return {
touchmove: {
callback(event) {
// ...
const Tracker = define(
'x-tracker',
class extends Component {
static get listeners() {
return {
touchmove: {
callback(event) {
// ...
},
target: window,
passive: true,
},
target: window,
passive: true,
},
};
};
}
}
}

define('x-tracker', Tracker);
);
```

:::
Expand Down Expand Up @@ -177,22 +181,24 @@ class Dialog extends builtin.HTMLDialogElement {
```ts [get listeners]
import { builtin, define } from '@chialab/dna';

class Dialog extends builtin.HTMLDialogElement {
static get listeners() {
return {
'click nav button': {
callback(event, target) {
// ...
const Dialog = define(
'x-dialog',
class extends builtin.HTMLDialogElement {
static get listeners() {
return {
'click nav button': {
callback(event, target) {
// ...
},
passive: false,
},
passive: false,
},
};
};
}
},
{
extends: 'dialog',
}
}

define('x-dialog', Dialog, {
extends: 'dialog',
});
);
```

:::
Expand Down
67 changes: 35 additions & 32 deletions docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,23 @@ class HelloWorld extends Component {
```ts [JavaScript]
import { Component, define, html } from '@chialab/dna';

class HelloWorld extends Component {
static get properties() {
return {
name: {
type: String,
defaultValue: '',
},
};
}

render() {
return html`<h1>Hello ${this.name || 'world'}!</h1>`;
const HelloWorld = define(
'hello-world',
class extends Component {
static get properties() {
return {
name: {
type: String,
defaultValue: '',
},
};
}

render() {
return html`<h1>Hello ${this.name || 'world'}!</h1>`;
}
}
}

define('hello-world', HelloWorld);
);
```

:::
Expand Down Expand Up @@ -138,24 +139,26 @@ class AlertDialog extends builtin.HTMLDialogElement {
```ts [JavaScript]
import { builtin, define, html } from '@chialab/dna';

class AlertDialog extends builtin.HTMLDialogElement {
static get properties() {
return {
title: {
type: String,
defaultValue: '',
},
};
const AlertDialog = define(
'alert-dialog',
class extends builtin.HTMLDialogElement {
static get properties() {
return {
title: {
type: String,
defaultValue: '',
},
};
}

render() {
return html`<h1>${this.title}</h1>`;
}
},
{
extends: 'dialog',
}

render() {
return html`<h1>${this.title}</h1>`;
}
}

define('alert-dialog', AlertDialog, {
extends: 'dialog',
});
);
```

:::
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Component, customElement } from '@chialab/dna';
import Pickr from '@simonwep/pickr';

@customElement('color-picker')
export class ColorPicker extends Component {
class ColorPicker extends Component {
private pickr: Pickr;

connectedCallback() {
Expand Down
77 changes: 40 additions & 37 deletions docs/guide/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ class Card extends Component {
```ts [get properties]
import { Component, define } from '@chialab/dna';

class Card extends Component {
static get properties() {
return {
age: Number,
};
const Card = define(
'x-card',
class extends Component {
static get properties() {
return {
age: Number,
};
}
}
}

define('x-card', Card);
);
```

:::
Expand Down Expand Up @@ -59,18 +60,19 @@ class Card extends Component {
```ts [get properties]
import { Component, define } from '@chialab/dna';

class Card extends Component {
static get properties() {
return {
collapsed: {
type: Boolean,
state: true,
},
};
const Card = define(
'x-card',
class extends Component {
static get properties() {
return {
collapsed: {
type: Boolean,
state: true,
},
};
}
}
}

define('x-card', Card);
);
```

:::
Expand Down Expand Up @@ -214,26 +216,27 @@ The **setter** function is invoked _before_ property validation and observers an
```ts
import { Component, define } from '@chialab/dna';

class Card extends Component {
static get properties() {
return {
phone: {
type: String,
getter(innerValue) {
return innerValue;
},
setter(value) {
if (!value.startsWith('+') && !value.startsWith('00')) {
value = `+44 ${value}`;
}
return value;
const Card = define(
'x-card',
class extends Component {
static get properties() {
return {
phone: {
type: String,
getter(innerValue) {
return innerValue;
},
setter(value) {
if (!value.startsWith('+') && !value.startsWith('00')) {
value = `+44 ${value}`;
}
return value;
},
},
},
};
};
}
}
}

define('x-card', Card);
);
```

## Observers
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Component, customElement, property } from '@chialab/dna';
* @fires move - The map center point changed.
*/
@customElement('x-map')
export class MapboxMap extends Component {
class MapboxMap extends Component {
/**
* The latitude value for the map center.
*/
Expand Down
Loading

0 comments on commit 0bb72a6

Please sign in to comment.