Skip to content

Commit a8c9e38

Browse files
committed
fix: apply css properties in shadow dom
1 parent d41a13b commit a8c9e38

File tree

8 files changed

+56
-17
lines changed

8 files changed

+56
-17
lines changed

.changeset/brave-pigs-carry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@msw-dev-tool/react": patch
3+
---
4+
5+
- Fix bug: tailwind's `@property` is not applied in shadow dom. So, an issue occurred where `border` and `shadow` not applied.
6+
- So, I parse css style and extract `@property`. And add these to shadow root's style sheet.

packages/react/postcss.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
plugins: {
3+
"@tailwindcss/postcss": {},
4+
},
5+
};

packages/react/src/style/msw-dev-tool.css

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
@layer theme, base, components, utilities;
2-
@import "tailwindcss/theme.css" layer(theme);
3-
@import "tailwindcss/utilities.css" layer(utilities);
1+
@import "tailwindcss";
42

53
@source inline("gap-1 gap-2 gap-3 gap-4 gap-5 gap-6 gap-7 gap-8 gap-9 gap-10 gap-11 gap-12");
64
@source inline("p-{1,2,3,4,5,6,7,8,9,10,11,12} px-{1,2,3,4,5,6,7,8,9,10,11,12} py-{1,2,3,4,5,6,7,8,9,10,11,12}");
@@ -29,7 +27,6 @@
2927
/* ===== Misc ===== */
3028
@source inline("bg-transparent underline");
3129

32-
3330
.msw-dt-sub-text {
3431
@apply text-sm text-gray-800;
3532
}
@@ -39,7 +36,9 @@
3936
}
4037

4138
.msw-dt-select-trigger {
42-
@apply inline-flex items-center justify-between rounded-md px-4 py-0 text-sm leading-none h-9 gap-1 border border-gray-300 text-gray-700 bg-white box-border;
39+
@apply inline-flex items-center justify-between rounded-md gap-1;
40+
@apply border border-gray-300 text-gray-700 bg-white;
41+
@apply px-4 py-0 text-sm leading-none h-9 box-border;
4342
}
4443

4544
.msw-dt-select-trigger:hover {
@@ -59,7 +58,8 @@
5958
}
6059

6160
.msw-dt-select-content {
62-
@apply overflow-hidden bg-white rounded-lg shadow-[0px_10px_38px_-10px_rgba(22,23,24,0.35),0px_10px_20px_-15px_rgba(22,23,24,0.2)];
61+
@apply overflow-hidden rounded-lg;
62+
@apply bg-white shadow-[0px_10px_38px_-10px_rgba(22,23,24,0.35),0px_10px_20px_-15px_rgba(22,23,24,0.2)]
6363
}
6464

6565
.msw-dt-select-viewport {

packages/react/src/style/trigger.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
.msw-dt-default-trigger svg {
2222
border: 1px solid #c7c7c7;
2323
border-radius: 1rem;
24-
color: #101010;
24+
color: #000;
25+
transition: color 0.2s ease-in-out;
2526
}
2627

2728
.msw-dt-default-trigger svg:hover {

packages/react/src/ui/Components/Logo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import React from "react";
22

33
export const MSWDevToolLogo = ({
44
size = "28",
5-
backgroundColor = "#101010",
6-
color = "#FF6600",
5+
backgroundColor = "#000",
6+
color = "#FF6A33",
77
className,
88
}: {
99
size?: string | number;

packages/react/src/ui/Components/Select.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const Select = forwardRef<HTMLButtonElement, SelectProps>(
4141
({ options, placeholder, label, id, style, className, ...rest }, ref) => {
4242
const container = usePortalContainer()
4343
return (
44-
<_Select data-theme="light" data-radix-color-scheme="light" {...rest}>
44+
<_Select {...rest}>
4545
<SelectTrigger
4646
className={clsx("msw-dt-select-trigger", className)}
4747
aria-label={label ?? "select"}
@@ -62,9 +62,6 @@ export const Select = forwardRef<HTMLButtonElement, SelectProps>(
6262
<SelectPortal container={container}>
6363
<SelectContent
6464
className="msw-dt-select-content"
65-
style={{ zIndex: 10000 }}
66-
data-theme="light"
67-
data-radix-color-scheme="light"
6865
>
6966
<SelectScrollUpButton className="msw-dt-select-scroll-button">
7067
<ChevronUpIcon />

packages/react/src/ui/ThemeProvider.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
//import { Theme } from "@radix-ui/themes";
2-
import React, { PropsWithChildren, forwardRef } from "react";
1+
import React, { PropsWithChildren, forwardRef, useEffect, useRef } from "react";
32
import root from "react-shadow";
43
import styles from "../style/msw-dev-tool.css";
4+
import { getCssPropertiesStyleSheet } from "../utils/style";
5+
6+
const shadowSheet = getCssPropertiesStyleSheet(styles);
57

68
export const ThemeProvider = forwardRef<HTMLDivElement, PropsWithChildren>(
79
({ children }, ref) => {
10+
const shadowHostRef = useRef<HTMLDivElement>(null);
11+
12+
/**
13+
* - `@property` is not supported in shadow dom.
14+
* - So we need to use `adoptedStyleSheets` to apply the properties forcefully.
15+
*/
16+
useEffect(() => {
17+
if (shadowHostRef?.current?.shadowRoot) {
18+
shadowHostRef.current.shadowRoot.adoptedStyleSheets = [shadowSheet];
19+
}
20+
}, []);
21+
822
return (
923
<>
10-
<root.div id="msw-dev-tool-shadow-root">
24+
<root.div id="msw-dev-tool-shadow-root" ref={shadowHostRef}>
1125
<div ref={ref} className="fixed z-[9999] inset-0">
12-
{ref && children}
26+
{children}
1327
</div>
28+
{/* Currently, size of styles is about 40kb. If it is over 50kb, We have to separate the styles. */}
1429
<style type="text/css">{styles}</style>
1530
</root.div>
1631
</>

packages/react/src/utils/style.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const getCssPropertiesStyleSheet = (styles: string) => {
2+
const shadowSheet = new CSSStyleSheet();
3+
shadowSheet.replaceSync(styles.replace(/:root/gu, ":host"));
4+
const properties = [];
5+
for (const rule of Array.from(shadowSheet.cssRules)) {
6+
if (rule instanceof CSSPropertyRule) {
7+
if (rule.initialValue) {
8+
properties.push(`${rule.name}: ${rule.initialValue}`);
9+
}
10+
}
11+
}
12+
13+
shadowSheet.insertRule(`:host { ${properties.join("; ")} }`);
14+
return shadowSheet;
15+
};

0 commit comments

Comments
 (0)