Skip to content

Commit 290d8b4

Browse files
committed
improve tag-name-input functionality
1 parent 8efe835 commit 290d8b4

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

css/learn.css

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,23 @@ main > h1 {
7474
border: var(--border-size-2) solid var(--gray-3);
7575
}
7676

77-
.interactive-input input ~ .icon {
77+
.interactive-input input ~ :where(.valid, .error, .hint) {
7878
display: none;
7979
}
8080

81-
.interactive-input input:invalid ~ :where(.error, .icon-false) {
81+
.interactive-input input:invalid ~ .error {
8282
color: var(--red-5);
83-
display: inline-block;
83+
display: block;
8484
}
8585

86-
.interactive-input input:not(:placeholder-shown):valid ~ :where(.error, .icon-true) {
86+
.interactive-input input:not(:placeholder-shown):valid ~ .valid {
8787
color: var(--green-5);
88-
display: inline-block;
88+
display: block;
89+
}
90+
91+
.interactive-input input ~ .hint:has(span:not(:empty)) {
92+
color: var(--blue-5);
93+
display: block;
8994
}
9095

9196
.content {

learn/components/naming-your-components.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ Try typing a tag name below to see if it's a valid custom element tag:
2323
<label class="interactive-input">
2424
Tag Name:
2525
<input type="text" is="tag-name-input" placeholder="fancy-button">
26-
<br>
27-
{% icon "false" %}{% icon "true" %}
28-
<span class="error"></span>
26+
<p class="valid">
27+
{% icon "true" %}
28+
<span>Valid custom element name!</span>
29+
</p>
30+
<p class="error">
31+
{% icon "false" %}
32+
<span></span>
33+
</p>
34+
<p class="hint">
35+
{% icon "info" %}
36+
<span></span>
37+
</p>
2938
</label>
3039

3140
### Some tag names are reserved

script/tag-name-input.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {validTagName, reservedTags, PCENChar } from 'eslint-plugin-custom-elements/lib/tag-names.js'
1+
import { validTagName, reservedTags, PCENChar, builtInTagMap } from 'eslint-plugin-custom-elements/lib/tag-names.js'
22

33
const validChar = new RegExp(`^${PCENChar}$`)
44

@@ -12,6 +12,7 @@ customElements.define('tag-name-input', class extends HTMLInputElement {
1212
handleEvent() {
1313
const {value} = this
1414
this.setCustomValidity('')
15+
let hint = ''
1516
if (value) {
1617
if (/[A-Z]/.test(value)) {
1718
this.setCustomValidity(`${value} is not valid, it cannot contain capital letters`)
@@ -28,12 +29,22 @@ customElements.define('tag-name-input', class extends HTMLInputElement {
2829
for (const char of value) {
2930
if (!(validChar.test(char))) chars.add(char)
3031
}
31-
this.setCustomValidity(`${value} is not a valid tag name, cannot contain ${[...chars].join(', ')}!`)
32+
this.setCustomValidity(`${value} is not a valid tag name, cannot contain ${[...chars].join(', ')}`)
3233
}
34+
35+
const parts = value.split(/-/g)
36+
for (const part in parts) {
37+
if (part in builtInTagMap) {
38+
hint = `${value} is similar to the built-in ${builtInTagMap[part]}`
39+
}
40+
}
41+
3342
}
3443
this.reportValidity()
35-
const errorEl = this.parentElement.querySelector('.error')
36-
errorEl.textContent = this.validationMessage || (value ? 'Valid custom element name!' : '')
44+
const errorEl = this.parentElement.querySelector('.error span')
45+
errorEl.textContent = this.validationMessage
46+
const hintEl = this.parentElement.querySelector('.hint span')
47+
hintEl.textContent = hint
3748
}
3849

3950
}, { extends: 'input' })

0 commit comments

Comments
 (0)