Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
nichoth committed Jul 13, 2024
1 parent 0e41c4e commit 7b6275b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ native `.dispatchEvent` method,
> returns `true` if either event's `cancelable` attribute value is false or its `preventDefault()` method was not invoked, and `false` otherwise.
That is, it returns true if it was not `preventDetault`ed.
```ts
dispatch (type:string, opts:Partial<{
bubbles,
Expand Down
4 changes: 2 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MyElement extends WebComponent.create('my-element') {
this.querySelector('button.namespaced')?.addEventListener('click', ev => {
ev.preventDefault()
ev.stopPropagation()
this.emit('click', { detail: 'some data' })
this.emit<string>('click', { detail: 'some data' })
})

this.querySelector('button.regular')?.addEventListener('click', ev => {
Expand All @@ -42,7 +42,7 @@ document.body.innerHTML += `
`

const el = document.querySelector('my-element')
debug('the namespaced event....', MyElement.event('aaa'))
debug('the namespaced event...', MyElement.event('aaa'))

el?.addEventListener('my-element:click', ev => {
debug('got a namespaced click', ev.detail)
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ export abstract class WebComponent extends HTMLElement {
* `{ bubbles: true, cancelable: true }`
* @returns {boolean}
*/
emit (
emit<T = any> (
type:string,
opts:Partial<{
bubbles:boolean,
cancelable:boolean,
detail:CustomEvent['detail']
detail:CustomEvent<T>['detail']
}> = {}
):boolean {
const namespace = this.NAME
const event = new CustomEvent(`${namespace}:${type}`, {
const event = new CustomEvent<T>(`${namespace}:${type}`, {
bubbles: (opts.bubbles === undefined) ? true : opts.bubbles,
cancelable: (opts.cancelable === undefined) ? true : opts.cancelable,
detail: opts.detail
Expand All @@ -42,10 +42,10 @@ export abstract class WebComponent extends HTMLElement {
/**
* Create and emit an event, no namespacing.
*/
dispatch (type:string, opts:Partial<{
bubbles,
cancelable,
detail
dispatch<T> (type:string, opts:Partial<{
bubbles:boolean,
cancelable:boolean,
detail:CustomEvent<T>['detail']
}>):boolean {
const event = new CustomEvent(type, {
bubbles: (opts.bubbles === undefined) ? true : opts.bubbles,
Expand Down
4 changes: 3 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfig } from 'vite'
import postcssNesting from 'postcss-nesting'
import cssnanoPlugin from 'cssnano'

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -15,7 +16,8 @@ export default defineConfig({
css: {
postcss: {
plugins: [
postcssNesting
postcssNesting,
cssnanoPlugin
],
},
},
Expand Down

0 comments on commit 7b6275b

Please sign in to comment.