Skip to content

Commit e6b6bda

Browse files
author
JoelCDL
committed
Toggle search field in header comp as a popover
1 parent 31f9736 commit e6b6bda

7 files changed

Lines changed: 179 additions & 12 deletions

File tree

package-lock.json

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@faker-js/faker": "^9.3.0",
3838
"@fortawesome/fontawesome-pro": "^6.7.2",
3939
"@marcotss/parcel-transformer-handlebars-json": "^1.0.0",
40+
"@oddbird/css-anchor-positioning": "^0.6.1",
4041
"@parcel/optimizer-data-url": "^2.15.2",
4142
"@parcel/resolver-glob": "^2.15.2",
4243
"@parcel/transformer-inline-string": "^2.15.2",

src/components/search.hbs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<form class="c-search" role="search" action="/">
2-
<input type="search" name="s" aria-label="Search the California Digital Library">
3-
<button>Search</button>
4-
</form>
1+
<div class="c-search">
2+
<form popover id="search-form" class="c-search-form" role="search" action="/">
3+
<input type="search" name="s" aria-label="Search the California Digital Library">
4+
<button>Search</button>
5+
</form>
6+
<button popovertarget="search-form" class="c-search-toggle" aria-label="Toggle search field"></button>
7+
</div>

src/css/components/header.css

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
padding: var(--uc3-space-lg);
88
gap: var(--uc3-space-base);
99
backdrop-filter: var(--text-backdrop-filter);
10+
11+
.c-search {
12+
justify-self: end;
13+
}
1014
}
1115

1216
.c-headernav {
@@ -18,11 +22,6 @@
1822
grid-template-columns: 15rem auto;
1923
grid-template-rows: 2.1rem auto;
2024
gap: var(--uc3-space-base) 4rem;
21-
22-
.c-search {
23-
justify-self: end;
24-
inline-size: 20rem;
25-
}
2625
}
2726

2827
.c-header__logo {

src/css/components/search.css

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,94 @@
22

33
.c-search {
44
display: flex;
5+
justify-content: flex-end;
6+
7+
&:focus-within { /* #1 */
8+
outline: 5px auto blue;
9+
outline: 5px auto -webkit-focus-ring-color;
10+
}
11+
}
12+
13+
.c-search-form {
14+
position-anchor: --my-anchor;
15+
inset: auto;
16+
right: anchor(left);
17+
align-self: anchor-center;
18+
width: 0; /* #2 */
19+
height: anchor-size(height); /* #3 */
20+
margin: 0;
21+
margin-inline-end: var(--uc3-space-base);
22+
padding: unset;
23+
transition-property: opacity, display, width, overlay;
24+
transition-duration: 0.3s;
25+
transition-behavior: allow-discrete;
526
border: 1px solid black;
27+
opacity: 0; /* #2 */
628

729
input {
8-
flex: 1 1 auto;
930
inline-size: 100%;
1031
padding: 0.3rem 0.6rem;
11-
border: none;
32+
border-style: none;
1233
}
1334

1435
button {
1536
padding-inline: 0.8rem;
16-
border: none;
37+
border-style: none;
1738
background-color: black;
1839
color: white;
40+
41+
&:focus {
42+
margin: 1px;
43+
background-color: white;
44+
color: black;
45+
}
46+
}
47+
48+
&:popover-open {
49+
display: flex;
50+
width: 45vw;
51+
opacity: 1;
52+
53+
@starting-style {
54+
width: 0;
55+
opacity: 0;
56+
}
57+
}
58+
}
59+
60+
.c-search-toggle {
61+
anchor-name: --my-anchor;
62+
block-size: 1.9rem;
63+
inline-size: 1.9rem;
64+
padding: unset;
65+
border-style: none;
66+
background-color: var(--uc3-color-dark-blue);
67+
mask: url('data-url:npm:fa-solid/magnifying-glass.svg') center / contain;
68+
}
69+
70+
@media (width > 45rem) {
71+
.c-search-form {
72+
&:popover-open {
73+
width: 35vw;
74+
75+
@starting-style { /* #4, 5 */
76+
width: 0;
77+
opacity: 0;
78+
}
79+
}
1980
}
2081
}
82+
83+
/*
84+
#1: Search toggle loses it's focus ring with mask style, so reapply
85+
focus ring to the component container when toggle is focused.
86+
87+
#2: Final state of the exit animation.
88+
89+
#3: Set the popover height to that of the anchor (c-search-toggle).
90+
91+
#4: Starting state for the animiation.
92+
93+
#5: When using this media query, the starting styles before
94+
the query are reset for some reason, so duplicate them here.
95+
*/

src/css/main.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
/* Global Variables */
99

1010
:root {
11+
/* Enables transition of width or height to max-content or auto: */
12+
13+
interpolate-size: allow-keywords;
14+
1115
/* Spacing: */
1216

1317
--uc3-space-base: 1rem;

src/js/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ***** Anchor Positioning Polyfill ***** //
2+
3+
// Required for positioning elements that use the popover API. See https://github.com/oddbird/css-anchor-positioning?tab=readme-ov-file#configuration
4+
5+
const anchorPositioningPolyfill = async () => {
6+
const { default: polyfill } = await import('@oddbird/css-anchor-positioning/dist/css-anchor-positioning-fn.js')
7+
8+
polyfill({
9+
elements: undefined,
10+
excludeInlineStyles: false,
11+
useAnimationFrame: false
12+
})
13+
}
14+
15+
if (!('anchorName' in document.documentElement.style)) {
16+
anchorPositioningPolyfill()
17+
}

0 commit comments

Comments
 (0)