Skip to content

Commit 48eee8c

Browse files
Improve browser support
Now it works not only in Chrome, but also in Safari and Firefox. Support for `popover` is much wider than support for style queries.
1 parent 9920b91 commit 48eee8c

File tree

2 files changed

+53
-79
lines changed

2 files changed

+53
-79
lines changed

src/color-chart/color-chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ const Self = class ColorChart extends ColorElement {
241241
) {
242242
// The Popover API is supported
243243
let popover = swatch._el.wrapper;
244-
popover.setAttribute("popover", "");
244+
// popover.setAttribute("popover", "");
245245

246246
// We need these for the popover to be correctly activated and positioned,
247247
// otherwise, it won't be on the top layer

src/color-swatch/color-swatch.css

Lines changed: 52 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ slot {
9494
}
9595

9696
[part="details"] {
97-
/*
98-
Why here and not in the style query below? Because Safari has issues with parsing nested rules inside style queries,
99-
we need to duplicate some rules to achieve the desired effect. Moving custom properties here allows us to make the code a bit DRYer.
100-
*/
10197
--_border-color: var(
10298
--border-color,
10399
color-mix(in oklab, buttonborder 20%, oklab(none none none / 0%))
@@ -127,51 +123,19 @@ slot {
127123
font-size: 80%;
128124
}
129125

130-
@container style(--details-style: compact) {
126+
/* Triangle pointer */
127+
&::before {
128+
content: "";
131129
position: absolute;
130+
top: 100%;
132131
left: 50%;
133-
z-index: 2;
134-
translate: -50% 0;
135-
bottom: 100%;
136-
margin-bottom: calc(var(--_pointer-height) * 0.8);
137-
width: var(--_details-popup-width);
138-
background: canvas;
139-
border: 1px solid var(--_border-color);
140-
padding: 0.6em 1em;
141-
border-radius: 0.2rem;
142-
box-shadow: 0 0.05em 1em -0.7em canvastext;
143-
transition: var(--_transition-duration) allow-discrete;
144-
transition-property: all, display;
145-
transition-delay: 0s, var(--_transition-duration);
146-
transform-origin: 50% calc(100% + var(--_pointer-height));
147-
148-
/* Triangle pointer */
149-
&::before {
150-
content: "";
151-
position: absolute;
152-
top: 100%;
153-
left: 50%;
154-
translate: -50% -50%;
155-
aspect-ratio: 1;
156-
height: calc(var(--_pointer-height) * sqrt(2));
157-
background: inherit;
158-
border: inherit;
159-
rotate: -45deg;
160-
clip-path: polygon(0 0, 0 100%, 100% 100%);
161-
}
162-
163-
/*
164-
More straightforward selector:
165-
&:not(:is(:host(:hover), :host(:focus-within), :host(:active), :host(:target), :host([open])) *)
166-
doesn't work in Safari!
167-
See https://bugs.webkit.org/show_bug.cgi?id=296577
168-
*/
169-
&:is(:host(:not(:hover):not(:focus-within):not(:active):not(:target):not([open])) *),
170-
&:is(:host([open="false"]) *) {
171-
display: none;
172-
opacity: 0;
173-
scale: 0;
174-
}
132+
translate: -50% -50%;
133+
aspect-ratio: 1;
134+
height: calc(var(--_pointer-height) * sqrt(2));
135+
background: inherit;
136+
border: inherit;
137+
rotate: -45deg;
138+
clip-path: polygon(0 0, 0 100%, 100% 100%);
175139
}
176140

177141
&[popover] {
@@ -188,7 +152,6 @@ slot {
188152
top: var(--_popover-top);
189153
translate: -50% -100%;
190154

191-
/* We need to duplicate these rules from the style query rule because Safari refuses to pick them up. */
192155
margin-bottom: calc(var(--_pointer-height) * 0.8);
193156
width: var(--_details-popup-width);
194157
background: canvas;
@@ -203,47 +166,58 @@ slot {
203166
}
204167

205168
&:not(:popover-open) {
206-
/* Hide the popover when not open. This is also a workaround for Safari */
207169
display: none;
208-
opacity: 0;
209-
scale: 0;
210170
}
211171
}
212172

213-
&[popover] {
214-
/* Make the triangle pointer visible */
215-
overflow: visible;
216-
217-
/* Bring the popover back on the screen */
218-
position: fixed;
219-
inset: unset;
220-
173+
@starting-style {
174+
/* Enable transitions */
221175
&:popover-open {
222-
/* And position it relative to the parent swatch */
223-
left: var(--_popover-left);
224-
top: var(--_popover-top);
225-
translate: -50% -100%;
226-
227-
/* We need to duplicate these rules from the style query rule because Safari refuses to pick them up. */
228-
margin-bottom: calc(var(--_pointer-height) * 0.8);
229-
width: var(--_details-popup-width);
230-
background: canvas;
231-
border: 1px solid var(--_border-color);
232-
padding: 0.6em 1em;
233-
border-radius: 0.2rem;
234-
box-shadow: 0 0.05em 1em -0.7em canvastext;
235-
transition: var(--_transition-duration) allow-discrete;
236-
transition-property: all, display;
237-
transition-delay: 0s, var(--_transition-duration);
238-
transform-origin: 50% calc(100% + var(--_pointer-height));
176+
opacity: 0;
177+
scale: 0;
239178
}
179+
}
180+
}
240181

241-
&:not(:popover-open) {
242-
/* Hide the popover when not open. This is also a workaround for Safari */
182+
@container style(--details-style: compact) {
183+
[part="details"]:not([popover]) {
184+
position: absolute;
185+
left: 50%;
186+
z-index: 2;
187+
translate: -50% 0;
188+
bottom: 100%;
189+
margin-bottom: calc(var(--_pointer-height) * 0.8);
190+
width: var(--_details-popup-width);
191+
background: canvas;
192+
border: 1px solid var(--_border-color);
193+
padding: 0.6em 1em;
194+
border-radius: 0.2rem;
195+
box-shadow: 0 0.05em 1em -0.7em canvastext;
196+
transition: var(--_transition-duration) allow-discrete;
197+
transition-property: all, display;
198+
transition-delay: 0s, var(--_transition-duration);
199+
transform-origin: 50% calc(100% + var(--_pointer-height));
200+
201+
/*
202+
More straightforward selector:
203+
&:not(:is(:host(:hover), :host(:focus-within), :host(:active), :host(:target), :host([open])) *)
204+
doesn't work in Safari!
205+
See https://bugs.webkit.org/show_bug.cgi?id=296577
206+
*/
207+
&:is(:host(:not(:hover):not(:focus-within):not(:active):not(:target):not([open])) *),
208+
&:is(:host([open="false"]) *) {
243209
display: none;
244210
opacity: 0;
245211
scale: 0;
246212
}
213+
214+
@starting-style {
215+
/* Enable transitions */
216+
& {
217+
opacity: 0;
218+
scale: 0;
219+
}
220+
}
247221
}
248222
}
249223

0 commit comments

Comments
 (0)