-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtailwind.tsx
92 lines (88 loc) · 2.24 KB
/
tailwind.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use client';
import { ShadowScopeConfig } from './context';
import { StyleSheet, css } from './css-utils';
import { Scope } from './scope';
import * as React from 'react';
export type TailwindProps = React.PropsWithChildren<{
/**
* The tag name of the custom element rendered by `<Tailwind>`
*
* @defaultValue `'react-shadow-scope'`
*/
tag?: keyof ReactShadowScope.CustomElements;
/**
* The relative path of the Tailwind stylesheet when serving the application.
*
* @defaultValue `'/tailwind.css'`
*/
href?: string;
/**
* Styles that will apply only while the Tailwind stylesheet is in the process of being fetched.
*
* @defaultValue `:host { visibility: hidden; }`
*/
pendingStyles?: StyleSheet,
/**
*
*/
customStyles?: StyleSheet;
/**
* Light DOM content reflected by the given template; this can be useful for excluding children from the scope.
*/
slottedContent?: React.ReactNode,
/**
* Configure this instance of `<Tailwind>`. (Overrides `ShadowScopeConfigProvider`)
*/
config?: ShadowScopeConfig,
}>;
/**
* Creates a shadow DOM encapsulated scope for Tailwind.
*
* @example
* ```tsx
* <Tailwind slottedContent={children}>
* <h1 className="text-slate-900 font-extrabold text-4xl">
* Hello From Shadow DOM
* </h1>
* <slot></slot>
* </Tailwind>
* ```
*/
export const Tailwind = React.forwardRef<HTMLElement, TailwindProps>(
(props, forwardedRef) => {
const {
children,
tag = 'react-shadow-scope',
href = '/tailwind.css',
customStyles,
slottedContent,
pendingStyles = css`:host { visibility: hidden; }`,
config,
...forwardedProps
} = props;
const transformForTailwind = (cssString: string) => {
return cssString.replace(
/(?:^|\s)(html)(?:[^-_a-z])/gi,
':host',
);
}
return (
<Scope
{...forwardedProps}
ref={forwardedRef}
tag={tag}
stylesheet={customStyles}
stylesheets={[]}
href={href}
hrefs={[]}
pendingStyles={pendingStyles}
normalize={false}
config={config}
slottedContent={slottedContent}
__transform={transformForTailwind}
>
{children}
</Scope>
);
},
);