Skip to content

Commit

Permalink
add tag method
Browse files Browse the repository at this point in the history
  • Loading branch information
nichoth committed May 5, 2024
1 parent 7cb32f6 commit d537441
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ npm install @bicycle-codes/tonic
## fork
This is a fork of [@socketsupply/tonic](https://github.com/socketsupply/tonic)

### additions
Things added to the forked version

#### `static get tag():string;`

Get the HTML tag name given a Tonic class.

```js
class ExampleTwo extends Tonic {
render () {
return this.html`<div>example two</div>`
}
}

ExampleTwo.tag
// => 'example-two'
```

## Use

```js
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"eslint": "^8.57.0",
"eslint-config-standard": "^17.1.0",
"tap-arc": "1.2.2",
"tap-spec": "5.0.0",
"tape-run": "^11.0.0",
"uuid": "^9.0.0"
},
Expand Down
7 changes: 4 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ export class Tonic<T = {}> extends HTMLElement {
static nonce: any;
static _createId(): string;
static _normalizeAttrs(o: any, x?: {}): {};
static match(el: any, s: any): any;
static getTagName(camelName: any): any;
static getPropertyNames(proto: any): string[];
static match(el:any, s:any):any;
static getTagName(camelName:string):string;
static get tag():string;
static getPropertyNames(proto:any):string[];
/**
* Add a component
* @param {typeof TonicComponent} c
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class Tonic extends window.HTMLElement {
return true
}

static get tag () {
return Tonic.getTagName(this.name)
}

static _createId () {
return `tonic${Tonic._index++}`
}
Expand Down
17 changes: 14 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ test('get kebab case from camel case', t => {
'example using a tonic component')
})

test('tagName instance method', t => {
class ExampleTwo extends Tonic {
render () {
return this.html`<div>example two</div>`
}
}

t.equal(ExampleTwo.tag, 'example-two',
'should return the correct tag name')
})

test('attach to dom', async t => {
class ComponentA extends Tonic {
render () {
Expand Down Expand Up @@ -979,9 +990,9 @@ test('pass in references to children', async t => {

render () {
return this.html`
${this.left}<br/>
${this.right}
`
${this.left}<br/>
${this.right}
`
}
}
Tonic.add(DividerComponent, cName)
Expand Down

0 comments on commit d537441

Please sign in to comment.