Skip to content

Commit 243afaf

Browse files
committed
get tailwind config file with reselect
1 parent d7da464 commit 243afaf

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

editor/src/core/tailwind/tailwind-compilation.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { createTailwindcss } from '@mhsdesign/jit-browser-tailwindcss'
44
import type { ProjectContentTreeRoot } from 'utopia-shared/src/types'
55
import { getProjectFileByFilePath, walkContentsTree } from '../../components/assets'
66
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'
812
import { importDefault } from '../es-modules/commonjs-interop'
913
import { rescopeCSSToTargetCanvasOnly } from '../shared/css-utils'
1014
import type { RequireFn } from '../shared/npm-dependency-types'
@@ -13,6 +17,8 @@ import { ElementsToRerenderGLOBAL } from '../../components/canvas/ui-jsx-canvas'
1317
import { isFeatureEnabled } from '../../utils/feature-switches'
1418
import type { Config } from 'tailwindcss/types/config'
1519
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'
1622

1723
const LatestConfig: { current: { code: string; config: Config } | null } = { current: null }
1824
export function getTailwindConfigCached(editorState: EditorState): Config | null {
@@ -103,6 +109,11 @@ function runTailwindClassGenerationOnDOMMutation(
103109
generateTailwindClasses(projectContents, requireFn)
104110
}
105111

112+
const tailwindConfigSelector = createSelector(
113+
(store: ProjectContentSubstate) => store.editor.projectContents,
114+
(projectContents) => getProjectFileByFilePath(projectContents, TailwindConfigPath),
115+
)
116+
106117
export const useTailwindCompilation = () => {
107118
const requireFnRef = useRefEditorState((store) => {
108119
const requireFn = store.editor.codeResultCache.curriedRequireFn(store.editor.projectContents)
@@ -114,11 +125,20 @@ export const useTailwindCompilation = () => {
114125
(store) => store.editor.canvas.interactionSession != null,
115126
)
116127

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+
)
119134

120135
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+
) {
122142
return
123143
}
124144

@@ -131,7 +151,7 @@ export const useTailwindCompilation = () => {
131151
)
132152
})
133153

134-
observer.observe(document.getElementById(CanvasContainerID)!, {
154+
observer.observe(canvasContainer, {
135155
attributes: true,
136156
childList: true,
137157
subtree: true,
@@ -143,5 +163,5 @@ export const useTailwindCompilation = () => {
143163
return () => {
144164
observer.disconnect()
145165
}
146-
}, [isInteractionActiveRef, projectContentsRef, requireFnRef, tailwindConfigExists])
166+
}, [isInteractionActiveRef, projectContentsRef, requireFnRef, tailwindConfig])
147167
}

0 commit comments

Comments
 (0)