@@ -4,7 +4,11 @@ import { createTailwindcss } from '@mhsdesign/jit-browser-tailwindcss'
4
4
import type { ProjectContentTreeRoot } from 'utopia-shared/src/types'
5
5
import { getProjectFileByFilePath , walkContentsTree } from '../../components/assets'
6
6
import { CanvasContainerID } from '../../components/canvas/canvas-types'
7
- import { useRefEditorState } from '../../components/editor/store/store-hook'
7
+ import {
8
+ Substores ,
9
+ useEditorState ,
10
+ useRefEditorState ,
11
+ } from '../../components/editor/store/store-hook'
8
12
import { importDefault } from '../es-modules/commonjs-interop'
9
13
import { rescopeCSSToTargetCanvasOnly } from '../shared/css-utils'
10
14
import type { RequireFn } from '../shared/npm-dependency-types'
@@ -13,6 +17,8 @@ import { ElementsToRerenderGLOBAL } from '../../components/canvas/ui-jsx-canvas'
13
17
import { isFeatureEnabled } from '../../utils/feature-switches'
14
18
import type { Config } from 'tailwindcss/types/config'
15
19
import type { EditorState } from '../../components/editor/store/editor-state'
20
+ import { createSelector } from 'reselect'
21
+ import type { ProjectContentSubstate } from '../../components/editor/store/store-hook-substore-types'
16
22
17
23
const LatestConfig : { current : { code : string ; config : Config } | null } = { current : null }
18
24
export function getTailwindConfigCached ( editorState : EditorState ) : Config | null {
@@ -103,6 +109,11 @@ function runTailwindClassGenerationOnDOMMutation(
103
109
generateTailwindClasses ( projectContents , requireFn )
104
110
}
105
111
112
+ const tailwindConfigSelector = createSelector (
113
+ ( store : ProjectContentSubstate ) => store . editor . projectContents ,
114
+ ( projectContents ) => getProjectFileByFilePath ( projectContents , TailwindConfigPath ) ,
115
+ )
116
+
106
117
export const useTailwindCompilation = ( ) => {
107
118
const requireFnRef = useRefEditorState ( ( store ) => {
108
119
const requireFn = store . editor . codeResultCache . curriedRequireFn ( store . editor . projectContents )
@@ -114,11 +125,20 @@ export const useTailwindCompilation = () => {
114
125
( store ) => store . editor . canvas . interactionSession != null ,
115
126
)
116
127
117
- const tailwindConfigExists =
118
- getProjectFileByFilePath ( projectContentsRef . current , TailwindConfigPath ) != null
128
+ // this is not a ref, beacuse we want to re-compile the Tailwind classes when the tailwind config changes
129
+ const tailwindConfig = useEditorState (
130
+ Substores . projectContents ,
131
+ tailwindConfigSelector ,
132
+ 'useTailwindCompilation tailwindConfig' ,
133
+ )
119
134
120
135
React . useEffect ( ( ) => {
121
- if ( ! tailwindConfigExists || ! isFeatureEnabled ( 'Tailwind' ) ) {
136
+ const canvasContainer = document . getElementById ( CanvasContainerID )
137
+ if (
138
+ tailwindConfig == null || // TODO: read this from the utopia key in package.json
139
+ canvasContainer == null ||
140
+ ! isFeatureEnabled ( 'Tailwind' )
141
+ ) {
122
142
return
123
143
}
124
144
@@ -131,7 +151,7 @@ export const useTailwindCompilation = () => {
131
151
)
132
152
} )
133
153
134
- observer . observe ( document . getElementById ( CanvasContainerID ) ! , {
154
+ observer . observe ( canvasContainer , {
135
155
attributes : true ,
136
156
childList : true ,
137
157
subtree : true ,
@@ -143,5 +163,5 @@ export const useTailwindCompilation = () => {
143
163
return ( ) => {
144
164
observer . disconnect ( )
145
165
}
146
- } , [ isInteractionActiveRef , projectContentsRef , requireFnRef , tailwindConfigExists ] )
166
+ } , [ isInteractionActiveRef , projectContentsRef , requireFnRef , tailwindConfig ] )
147
167
}
0 commit comments