Skip to content

Commit 48296e0

Browse files
committed
Fix bug where style override loses to default styles for class
== npm run size == JS transerSize (compressed): * Before : 2248 * After : 2245 (-3 B) CSS: * Before : 1921 * After : 1954 (+33B)
1 parent 32ed625 commit 48296e0

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Contribute to typesense-minibar
22

3+
## Implementation notes
4+
5+
* The styles for `typesense-minibar` as web component, and `.tsmb-form` class name are kept independent (that is, the web component does not auto-add the class name).
6+
7+
This is done for two reasons:
8+
9+
1. Avoid selector conflict for Style API.
10+
If we were to add `class="tsmb-form"` in the web component, it would mean `typesense-minibar form` and `.tsmb-form` both match. This makes the `typesense-minibar form` selector impsosible to override in CSS for downstream users, because our defaults for `.tsmb-form` (weight 0010) would continue to "win" the cascade, as being a stronger selector than `typesense-minibar form` (weight 0002).
11+
2. Avoid a FOUC.
12+
The element should render identically and without reflows both before and after JavaScript loads. During local development it's easy to miss a FOUC if it fixes itself once JavaScript loads. By not auto-correcting these, the bugs are more obvious and we fix them.
13+
314
## Internal API
415

516
```js

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ Notes:
191191

192192
If you create or insert the element dynamically with JavaScript, it is recommended to write the form as a web component instead, like so:
193193
```html
194-
<typescript-minibar>
194+
<typesense-minibar>
195195
<form ..>..</form>
196-
</typescript-minibar>
196+
</typesense-minibar>
197197
```
198198

199199
Web components automatically activate the relevant JavaScript, no matter when they are inserted on the page.
200200

201-
By default, typescript-minibar.js also makes sure that any `<form class="tsmb-form">` elements on the page are hydrated and activated. This should catch any static element on the page (i.e. before "document ready", or the DOMContentLoaded event). This works internally by levering the fact that script execution is naturally deferred until the document is ready, via the `defer` and `type="module"` attributes on the `<script>` tag.
201+
By default, typesense-minibar.js also makes sure that any `<form class="tsmb-form">` elements on the page are hydrated and activated. This should catch any static element on the page (i.e. before "document ready", or the DOMContentLoaded event). This works internally by levering the fact that script execution is naturally deferred until the document is ready, via the `defer` and `type="module"` attributes on the `<script>` tag.
202202

203203
* How does this prevent JavaScript errors in older browsers? What about ES5?
204204

demo/demo-theme.css

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,23 @@ header > * {
2121
background-image: url(../assets/logo-text-dark.svg);
2222
}
2323

24-
header .tsmb-form {
25-
width: 30em;
24+
header .tsmb-form,
25+
header typesense-minibar {
2626
--tsmb-color-base-background: #691c69;
2727
--tsmb-color-base30: var(--tsmb-color-primary90);
2828
--tsmb-color-base50: #c090c0;
2929
--tsmb-color-base90: #c090c0;
3030
--tsmb-size-listbox-width: calc(min(50rem, 80vw));
3131
}
32-
header .tsmb-form:not(:focus-within)::before {
32+
header .tsmb-form,
33+
header typesense-minibar form {
34+
width: 30em;
35+
}
36+
header .tsmb-form:not(:focus-within)::before,
37+
header typesense-minibar form:not(:focus-within)::before {
3338
filter: invert();
3439
}
35-
header .tsmb-form input[type=search] {
40+
header .tsmb-form input[type=search],
41+
header typesense-minibar input[type=search] {
3642
border: none;
3743
}

demo/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
<input type="hidden" name="sites" value="jquery.com">
2727
</form>
2828
</header>
29+
<header>
30+
<span class="demo-logo"></span>
31+
<!-- Web Component -->
32+
<typesense-minibar class="demo-right-form">
33+
<form role="search" data-origin="https://typesense.jquery.com" data-collection="jquery_com" data-key="Zh8mMgohXECel9wjPwqT7lekLSG3OCgz" data-foot="true" data-group="true" action="https://duckduckgo.com">
34+
<input type="search" name="q" aria-label="Search" placeholder="Search..." autocomplete="off">
35+
<input type="hidden" name="sites" value="jquery.com">
36+
</form>
37+
</typesense-minibar>
38+
</header>
2939
<main>
3040
<h2>Web Component</h2>
3141
<typesense-minibar>

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ QUnit.module('typesense-minibar', hooks => {
535535
const element = parseHTML('<typesense-minibar><form><input type="search"></form></typesense-minibar>');
536536
document.querySelector('#qunit-fixture').append(element);
537537
const form = element.querySelector('form');
538-
assert.equal(form.className, 'tsmb-form tsmb-form--slash', 'set form class');
538+
assert.equal(form.className, 'tsmb-form--slash', 'set form class');
539539

540540
const input = form.firstChild;
541541
const listbox = element.querySelector('[role=listbox]');

typesense-minibar.css

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ typesense-minibar form::before {
142142
color: var(--tsmb-color-base90);
143143
}
144144

145-
.tsmb-form [role=listbox] {
145+
.tsmb-form [role=listbox],
146+
typesense-minibar [role=listbox] {
146147
position: absolute;
147148
z-index: 10;
148149
right: var(--tsmb-size-listbox-right);
@@ -163,29 +164,35 @@ typesense-minibar form::before {
163164
border-bottom: var(--tsmb-size-edge) solid var(--tsmb-color-focus90);
164165
}
165166

166-
.tsmb-form [role=option] a {
167+
.tsmb-form [role=option] a,
168+
typesense-minibar [role=option] a {
167169
display: block;
168170
padding: var(--tsmb-size-base);
169171
text-decoration: none;
170172
border-left: var(--tsmb-size-highlight) solid transparent;
171173
}
172174

173-
.tsmb-form:not([data-group=true]) [role=option]:not(:first-child) a {
175+
.tsmb-form:not([data-group=true]) [role=option]:not(:first-child) a,
176+
typesense-minibar form:not([data-group=true]) [role=option]:not(:first-child) a {
174177
border-top: var(--tsmb-size-edge) solid var(--tsmb-color-focus90);
175178
}
176179

177-
.tsmb-form[data-group=true] [role=option] a {
180+
.tsmb-form[data-group=true] [role=option] a,
181+
typesense-minibar form[data-group=true] [role=option] a {
178182
margin: 0 var(--tsmb-size-base);
179183
padding: var(--tsmb-size-sm);
180184
}
181185

182186
.tsmb-form [role=option] a:hover,
183-
.tsmb-form [role=option][aria-selected=true] a {
187+
.tsmb-form [role=option][aria-selected=true] a,
188+
typesense-minibar [role=option] a:hover,
189+
typesense-minibar [role=option][aria-selected=true] a {
184190
background: var(--tsmb-color-primary90);
185191
border-left-color: var(--tsmb-color-primary50);
186192
}
187193

188-
.tsmb-form [role=option] mark {
194+
.tsmb-form [role=option] mark,
195+
typesense-minibar [role=option] mark {
189196
background: none;
190197
color: inherit;
191198
font-style: normal;

typesense-minibar.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ globalThis.tsminibar = function tsminibar (form) {
9292

9393
function connect () {
9494
document.addEventListener('click', onDocClick);
95-
form.classList.add('tsmb-form');
9695
if (form.dataset.slash !== 'false') {
9796
document.addEventListener('keydown', onDocSlash);
9897
form.classList.add('tsmb-form--slash');

0 commit comments

Comments
 (0)