Skip to content

Commit

Permalink
feat: selector read from filename
Browse files Browse the repository at this point in the history
  • Loading branch information
infodusha committed Sep 7, 2023
1 parent 60af603 commit 882dc0c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Add link to preload external html file and define-html script in `head`:
```html
<head>
<!-- Other tags here -->
<link rel="preload" href="content.html" as="fetch" crossorigin />
<link rel="preload" href="app-content.html" as="fetch" crossorigin />
<script src="https://unpkg.com/define-html" type="module"></script>
</head>
```
Where `content.html` is:
Where `app-content.html` is:
```html
<template data-selector="app-content">
<template>
Lorem ipsum
</template>
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<template data-selector="app-root">
<template>
<app-header></app-header>
<div class="cards">
<app-card header="Easy to install">
Expand Down
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<title>define-html</title>
<link rel="preload" href="components/root.html" as="fetch" crossorigin />
<link rel="preload" href="components/app-root.html" as="fetch" crossorigin />
<link rel="preload" href="components/card.html" as="fetch" crossorigin />
<link rel="preload" href="header.html" as="fetch" crossorigin />
<link rel="preload" href="footer.html" as="fetch" crossorigin />
Expand Down
3 changes: 2 additions & 1 deletion src/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface Disconnected {

export function createComponent(definedElement: Document, relativeTo: string): [string, typeof HTMLElement] {
const template = returnIfDefined(definedElement.querySelector('template'), 'Template is required');
const selector = returnIfDefined(template.getAttribute('data-selector'), 'data-selector is required');
const filename = returnIfDefined(relativeTo.split('/').pop()).replace(/\.html$/, '');
const selector = template.getAttribute('data-selector') ?? filename;
const useShadow = template.hasAttribute('data-shadow');

const styles: NodeListOf<HTMLStyleElement> = definedElement.querySelectorAll('style:not([data-global])');
Expand Down

0 comments on commit 882dc0c

Please sign in to comment.