Skip to content

Commit 9920b91

Browse files
[color-swatch] Fix popovers with color details in <color-chart>s in Safari
Safari appears to have issues with combining styles within container queries with popover styles, so we need to move them out.
1 parent 3539e6f commit 9920b91

File tree

1 file changed

+85
-23
lines changed

1 file changed

+85
-23
lines changed

src/color-swatch/color-swatch.css

Lines changed: 85 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ 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+
*/
101+
--_border-color: var(
102+
--border-color,
103+
color-mix(in oklab, buttonborder 20%, oklab(none none none / 0%))
104+
);
105+
--_pointer-height: var(--pointer-height, 0.5em);
106+
--_transition-duration: var(--transition-duration, 400ms);
107+
--_details-popup-width: var(--details-popup-width, max-content);
108+
97109
display: flex;
98110
flex-flow: inherit;
99111
gap: inherit;
@@ -116,14 +128,6 @@ slot {
116128
}
117129

118130
@container style(--details-style: compact) {
119-
--_border-color: var(
120-
--border-color,
121-
color-mix(in oklab, buttonborder 20%, oklab(none none none / 0%))
122-
);
123-
--_pointer-height: var(--pointer-height, 0.5em);
124-
--_transition-duration: var(--transition-duration, 400ms);
125-
--_details-popup-width: var(--details-popup-width, max-content);
126-
127131
position: absolute;
128132
left: 50%;
129133
z-index: 2;
@@ -135,26 +139,12 @@ slot {
135139
border: 1px solid var(--_border-color);
136140
padding: 0.6em 1em;
137141
border-radius: 0.2rem;
138-
box-shadow: 0 0.05em 1em -0.7em black;
142+
box-shadow: 0 0.05em 1em -0.7em canvastext;
139143
transition: var(--_transition-duration) allow-discrete;
140144
transition-property: all, display;
141145
transition-delay: 0s, var(--_transition-duration);
142146
transform-origin: 50% calc(100% + var(--_pointer-height));
143147

144-
&[popover] {
145-
/* Make the triangle pointer visible */
146-
overflow: visible;
147-
148-
/* Bring the popover back on the screen */
149-
position: fixed;
150-
inset: unset;
151-
152-
/* And position it relative to the parent swatch */
153-
left: var(--_popover-left);
154-
top: var(--_popover-top);
155-
translate: -50% -100%;
156-
}
157-
158148
/* Triangle pointer */
159149
&::before {
160150
content: "";
@@ -183,6 +173,78 @@ slot {
183173
scale: 0;
184174
}
185175
}
176+
177+
&[popover] {
178+
/* Make the triangle pointer visible */
179+
overflow: visible;
180+
181+
/* Bring the popover back on the screen */
182+
position: fixed;
183+
inset: unset;
184+
185+
&:popover-open {
186+
/* And position it relative to the parent swatch */
187+
left: var(--_popover-left);
188+
top: var(--_popover-top);
189+
translate: -50% -100%;
190+
191+
/* We need to duplicate these rules from the style query rule because Safari refuses to pick them up. */
192+
margin-bottom: calc(var(--_pointer-height) * 0.8);
193+
width: var(--_details-popup-width);
194+
background: canvas;
195+
border: 1px solid var(--_border-color);
196+
padding: 0.6em 1em;
197+
border-radius: 0.2rem;
198+
box-shadow: 0 0.05em 1em -0.7em canvastext;
199+
transition: var(--_transition-duration) allow-discrete;
200+
transition-property: all, display;
201+
transition-delay: 0s, var(--_transition-duration);
202+
transform-origin: 50% calc(100% + var(--_pointer-height));
203+
}
204+
205+
&:not(:popover-open) {
206+
/* Hide the popover when not open. This is also a workaround for Safari */
207+
display: none;
208+
opacity: 0;
209+
scale: 0;
210+
}
211+
}
212+
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+
221+
&: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));
239+
}
240+
241+
&:not(:popover-open) {
242+
/* Hide the popover when not open. This is also a workaround for Safari */
243+
display: none;
244+
opacity: 0;
245+
scale: 0;
246+
}
247+
}
186248
}
187249

188250
[part="color"] {

0 commit comments

Comments
 (0)