Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow CustomElements to be redefined #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 30, 2022
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
{
"path": "lib/index.js",
"import": "{controller, attr, target, targets}",
"limit": "1.6kb"
"limit": "1.64kb"
}
]
}
13 changes: 10 additions & 3 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import {dasherize} from './dasherize.js'
*/
export function register(classObject: CustomElement): void {
const name = dasherize(classObject.name).replace(/-element$/, '')
if (!window.customElements.get(name)) {

try {
window.customElements.define(name, classObject)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window[classObject.name] = classObject
window.customElements.define(name, classObject)
window[classObject.name] = customElements.get(name)
} catch (e: unknown) {
// The only reason for window.customElements.define to throw a `NotSupportedError`
// is if the element has already been defined.
if (e instanceof DOMException && e.name === 'NotSupportedError') return

throw e
}
}