Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary Tailwind class compilation calls #6534

Merged
merged 9 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions editor/src/core/tailwind/tailwind-compilation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import type { TailwindConfig, Tailwindcss } from '@mhsdesign/jit-browser-tailwindcss'
import { createTailwindcss } from '@mhsdesign/jit-browser-tailwindcss'
import type { ProjectContentTreeRoot, TextFile, TextFileContents } from 'utopia-shared/src/types'
import type { ProjectContentTreeRoot } from 'utopia-shared/src/types'
import { getProjectFileByFilePath, walkContentsTree } from '../../components/assets'
import { interactionSessionIsActive } from '../../components/canvas/canvas-strategies/interaction-state'
import { CanvasContainerID } from '../../components/canvas/canvas-types'
Expand Down Expand Up @@ -87,6 +87,22 @@ function generateTailwindClasses(projectContents: ProjectContentTreeRoot, requir
void generateTailwindStyles(tailwindCss, allCSSFiles)
}

function runTailwindClassGenerationOnDOMMutation(
mutations: MutationRecord[],
projectContents: ProjectContentTreeRoot,
requireFn: RequireFn,
) {
const updateHasNewTailwindData = mutations.some(
(m) =>
m.addedNodes.length > 0 || // new DOM element was added with potentially new classes
m.attributeName === 'class', // potentially new classes were added to the class attribute of an element
)
if (!updateHasNewTailwindData) {
return
}
generateTailwindClasses(projectContents, requireFn)
}

export const useTailwindCompilation = (requireFn: RequireFn) => {
const projectContents = useEditorState(
Substores.projectContents,
Expand All @@ -98,34 +114,30 @@ export const useTailwindCompilation = (requireFn: RequireFn) => {
interactionSessionIsActive(store.editor.canvas.interactionSession),
)

const observerCallback = React.useCallback(() => {
React.useEffect(() => {
const tailwindConfigFile = getProjectFileByFilePath(projectContents, TailwindConfigPath)
if (
tailwindConfigFile == null ||
isInteractionActiveRef.current ||
ElementsToRerenderGLOBAL.current !== 'rerender-all-elements' || // implies that an interaction is in progress
!isFeatureEnabled('Tailwind')
) {
return
}
generateTailwindClasses(projectContents, requireFn)
}, [isInteractionActiveRef, projectContents, requireFn])

React.useEffect(() => {
const tailwindConfigFile = getProjectFileByFilePath(projectContents, TailwindConfigPath)
if (tailwindConfigFile == null || tailwindConfigFile.type !== 'TEXT_FILE') {
return // we consider tailwind to be enabled if there's a tailwind config file in the project
}
const observer = new MutationObserver(observerCallback)
const observer = new MutationObserver((mutations) => {
runTailwindClassGenerationOnDOMMutation(mutations, projectContents, requireFn)
})

observer.observe(document.getElementById(CanvasContainerID)!, {
attributes: true,
childList: true,
subtree: true,
})

observerCallback()
generateTailwindClasses(projectContents, requireFn)

return () => {
observer.disconnect()
}
}, [isInteractionActiveRef, observerCallback, projectContents, requireFn])
}, [isInteractionActiveRef, projectContents, requireFn])
}
170 changes: 170 additions & 0 deletions editor/src/core/tailwind/tailwind.spec.browser2.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { mouseClickAtPoint } from '../../components/canvas/event-helpers.test-utils'
import type { EditorRenderResult } from '../../components/canvas/ui-jsx.test-utils'
import { renderTestEditorWithModel } from '../../components/canvas/ui-jsx.test-utils'
import { switchEditorMode } from '../../components/editor/actions/action-creators'
import { EditorModes } from '../../components/editor/editor-modes'
import { StoryboardFilePath } from '../../components/editor/store/editor-state'
import { createModifiedProject } from '../../sample-projects/sample-project-utils.test-utils'
import { setFeatureForBrowserTestsUseInDescribeBlockOnly } from '../../utils/utils.test-utils'
import { windowPoint } from '../shared/math-utils'
import { TailwindConfigPath } from './tailwind-config'
import { Project } from './tailwind.test-utils'

describe('rendering tailwind projects in the editor', () => {
Expand Down Expand Up @@ -133,4 +141,166 @@ describe('rendering tailwind projects in the editor', () => {
})
}
})

describe('Remix', () => {
const projectWithMultipleRoutes = createModifiedProject({
[StoryboardFilePath]: `import * as React from 'react'
import { RemixScene, Storyboard } from 'utopia-api'

export var storyboard = (
<Storyboard data-uid='storyboard'>
<RemixScene
className='absolute top-[100px] left-[200px] w-[700px] h-[700px]'
data-label='Playground'
data-uid='remix'
/>
</Storyboard>
)
`,
['/app/root.js']: `import React from 'react'
import { Outlet } from '@remix-run/react'

export default function Root() {
return (
<div data-testid='root' className='flex flex-col gap-10 bg-red-200 text-2xl'>
I am Root!
<Outlet />
</div>
)
}
`,
['/app/routes/_index.js']: `import React from 'react'
import { Link } from '@remix-run/react'

export default function Index() {
return (
<div data-testid='index' className='flex flex-col gap-8'>
Index page
<Link to='/about' data-testid='remix-link'>About</Link>
</div>
)
}
`,
['/app/routes/about.js']: `import React from 'react'

export default function About() {
return (
<div data-testid='about' className='flex flex-row gap-6 p-4'>
<span data-testid='about-text' className='text-shadow-md'>About page</span>
</div>
)
}
`,
'/src/app.css': `
@tailwind base;
@tailwind components;
@tailwind utilities;
`,
[TailwindConfigPath]: `
const Tailwind = {
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
white: '#ffffff',
purple: '#3f3cbb',
midnight: '#121063',
metal: '#565584',
tahiti: '#3ab7bf',
silver: '#ecebff',
'bubble-gum': '#ff77e9',
bermuda: '#78dcca',
},
},
plugins: [
function ({ addUtilities }) {
const newUtilities = {
'.text-shadow': {
textShadow: '2px 2px 4px rgba(0, 0, 0, 0.1)',
},
'.text-shadow-md': {
textShadow: '3px 3px 6px rgba(0, 0, 0, 0.2)',
},
'.text-shadow-lg': {
textShadow: '4px 4px 8px rgba(0, 0, 0, 0.3)',
},
'.text-shadow-none': {
textShadow: 'none',
},
}

addUtilities(newUtilities, ['responsive', 'hover'])
},
],
}
export default Tailwind`,
})

it('can render content in a RemixScene', async () => {
const editor = await renderTestEditorWithModel(
projectWithMultipleRoutes,
'await-first-dom-report',
)
{
const root = editor.renderedDOM.getByTestId('root')
const { backgroundColor, display, flexDirection, gap, fontSize } = getComputedStyle(root)
expect({ backgroundColor, display, flexDirection, gap, fontSize }).toEqual({
backgroundColor: 'rgba(0, 0, 0, 0)',
display: 'flex',
flexDirection: 'column',
fontSize: '24px',
gap: '40px',
})
}
{
const index = editor.renderedDOM.getByTestId('index')
const { display, flexDirection, gap } = getComputedStyle(index)
expect({ display, flexDirection, gap }).toEqual({
display: 'flex',
flexDirection: 'column',
gap: '32px',
})
}
})
it('can render content after navigating to a different page', async () => {
const editor = await renderTestEditorWithModel(
projectWithMultipleRoutes,
'await-first-dom-report',
)
await switchToLiveMode(editor)
await clickRemixLink(editor)

{
const about = editor.renderedDOM.getByTestId('about')
const { display, flexDirection, gap, padding } = getComputedStyle(about)
expect({ display, flexDirection, gap, padding }).toEqual({
display: 'flex',
flexDirection: 'row',
gap: '24px',
padding: '16px',
})
}
{
const aboutText = editor.renderedDOM.getByTestId('about-text')
const { textShadow } = getComputedStyle(aboutText)
expect(textShadow).toEqual('rgba(0, 0, 0, 0.2) 3px 3px 6px')
}
})
})
})

const switchToLiveMode = (editor: EditorRenderResult) =>
editor.dispatch([switchEditorMode(EditorModes.liveMode())], true)

async function clickLinkWithTestId(editor: EditorRenderResult, testId: string) {
const targetElement = editor.renderedDOM.queryAllByTestId(testId)[0]
const targetElementBounds = targetElement.getBoundingClientRect()

const clickPoint = windowPoint({ x: targetElementBounds.x + 5, y: targetElementBounds.y + 5 })
await mouseClickAtPoint(targetElement, clickPoint)
}

async function clickRemixLink(editor: EditorRenderResult) {
await clickLinkWithTestId(editor, 'remix-link')
await editor.getDispatchFollowUpActionsFinished()
}
Loading