Skip to content

Commit

Permalink
feat: data-selector is not used anymore
Browse files Browse the repository at this point in the history
BREAKING CHANGE: data-selector is obsolete, just always use filename
  • Loading branch information
infodusha committed Jul 28, 2024
1 parent 9eb730f commit 0b7ee2d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
12 changes: 5 additions & 7 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ You can use `<template>` tag to define your component:
</template>
```

That is the only required element. That tag can be uses only once per file.
That is the only required element. The tag can be used only once per file.

You can use `<template data-selector="app-root">` to set selector for the component. If not specified, filename is used.

Must include dash `-` and be unique.
Filename is used as a selector for the component. It must include dash `-` and be unique.

You can use `<template data-shadow="closed">` to enable [shadow DOM](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM#encapsulation_from_css).

Expand Down Expand Up @@ -72,7 +70,7 @@ Inside you can use [:host](https://developer.mozilla.org/en-US/docs/Web/CSS/:hos
Inside your template you can use attributes to read component values itself (value would be set as child text):

```html
<template data-selector="app-title">
<template>
<h1 data-attr="title"></h1>
</template>
```
Expand Down Expand Up @@ -112,7 +110,7 @@ You can use `data-if-equal` attribute along with `data-if` to check if attribute
You can use `<slot>` tag to define a place for elements, that are nested inside the component:

```html
<template data-selector="app-card">
<template>
<div class="card">
<slot></slot>
</div>
Expand All @@ -130,7 +128,7 @@ So later you can use it like this:
You can use `<slot name="header">` to have multiple slots:

```html
<template data-selector="app-card">
<template>
<slot name="header"></slot>
<div class="card">
<slot></slot>
Expand Down
2 changes: 1 addition & 1 deletion example/footer.html → example/app-footer.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<template data-selector="app-footer" data-shadow="closed">
<template data-shadow="closed">
<footer>Built by infodusha. Inspired by Vika.</footer>
</template>

Expand Down
2 changes: 1 addition & 1 deletion example/header.html → example/app-header.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<template data-selector="app-header">
<template>
<header>
<h1>define-html</h1>
<span class="counter">0</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<template data-selector="app-card">
<template>
<slot name="header">
<h3 data-attr="header">No header</h3>
</slot>
Expand Down
11 changes: 8 additions & 3 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
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 />
<link
rel="preload"
href="components/app-card.html"
as="fetch"
crossorigin
/>
<link rel="preload" href="app-header.html" as="fetch" crossorigin />
<link rel="preload" href="app-footer.html" as="fetch" crossorigin />
<script src="../dist/index.js" type="module"></script>
</head>
<body>
Expand Down
3 changes: 1 addition & 2 deletions src/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ export function createComponent(
definedElement.querySelector("template"),
"Template is required"
);
const filename = returnIfDefined(relativeTo.split("/").pop()).replace(
const selector = returnIfDefined(relativeTo.split("/").pop()).replace(
/\.html$/,
""
);
const selector = template.getAttribute("data-selector") ?? filename;
const useShadow = template.hasAttribute("data-shadow");

const styles: NodeListOf<HTMLStyleElement> =
Expand Down

0 comments on commit 0b7ee2d

Please sign in to comment.