-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more granular unparsable prop detection for
StyleInfo
(#6631)
## Problem The `StyleInfo` type doesn't express the state when a property is set, but it cannot parsed in the desired format. For example, in `style={{ top: '300px', left: theme.left + 50 }}`, `top` can be parsed as a css number, but left cannot be. We need to be able to express this, since some code (`AdjustCssLengthProperties` and `SetCSSLength` for example) need to handle it. Once `AdjustCssLengthProperties` and `SetCSSLength` use this infra, we can make more strategies Tailwind-compatible. ## Fix Extend the `StyleInfo` type, and fix any affected code. **Manual Tests:** I hereby swear that: - [x] I opened a hydrogen project and it loaded - [x] I could navigate to various routes in Play mode
- Loading branch information
Showing
5 changed files
with
198 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
editor/src/components/canvas/plugins/inline-style-plugin.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as EP from '../../../core/shared/element-path' | ||
import { cssNumber } from '../../inspector/common/css-utils' | ||
import { | ||
cssStyleProperty, | ||
cssStylePropertyNotFound, | ||
cssStylePropertyNotParsable, | ||
} from '../canvas-types' | ||
import type { EditorRenderResult } from '../ui-jsx.test-utils' | ||
import { renderTestEditorWithCode } from '../ui-jsx.test-utils' | ||
import { InlineStylePlugin } from './inline-style-plugin' | ||
|
||
describe('inline style plugin', () => { | ||
it('can parse style info from element', async () => { | ||
const editor = await renderTestEditorWithCode( | ||
` | ||
import React from 'react' | ||
import { Scene, Storyboard } from 'utopia-api' | ||
export var storyboard = ( | ||
<Storyboard data-uid='sb'> | ||
<Scene | ||
id='scene' | ||
commentId='scene' | ||
data-uid='scene' | ||
style={{ | ||
width: 700, | ||
height: 759, | ||
position: 'absolute', | ||
left: 212, | ||
top: 128, | ||
}} | ||
> | ||
<div | ||
data-uid='div' | ||
style={{ display: 'flex', flexDirection: 'column', gap: '2rem'}} | ||
/> | ||
</Scene> | ||
</Storyboard> | ||
) | ||
`, | ||
'await-first-dom-report', | ||
) | ||
|
||
const styleInfo = getStyleInfoFromInlineStyle(editor) | ||
|
||
expect(styleInfo).not.toBeNull() | ||
const { flexDirection, gap } = styleInfo! | ||
expect(flexDirection).toEqual(cssStyleProperty('column')) | ||
expect(gap).toEqual(cssStyleProperty(cssNumber(2, 'rem'))) | ||
}) | ||
|
||
it('can parse style info with missing/unparsable props', async () => { | ||
const editor = await renderTestEditorWithCode( | ||
` | ||
import React from 'react' | ||
import { Scene, Storyboard } from 'utopia-api' | ||
const gap = { small: '1rem' } | ||
export var storyboard = ( | ||
<Storyboard data-uid='sb'> | ||
<Scene | ||
id='scene' | ||
commentId='scene' | ||
data-uid='scene' | ||
style={{ | ||
width: 700, | ||
height: 759, | ||
position: 'absolute', | ||
left: 212, | ||
top: 128, | ||
}} | ||
> | ||
<div | ||
data-uid='div' | ||
style={{ display: 'flex', gap: gap.small }} | ||
/> | ||
</Scene> | ||
</Storyboard> | ||
) | ||
`, | ||
'await-first-dom-report', | ||
) | ||
|
||
const styleInfo = getStyleInfoFromInlineStyle(editor) | ||
|
||
expect(styleInfo).not.toBeNull() | ||
const { flexDirection, gap } = styleInfo! | ||
expect(flexDirection).toEqual(cssStylePropertyNotFound()) | ||
expect(gap).toEqual(cssStylePropertyNotParsable()) | ||
}) | ||
}) | ||
|
||
function getStyleInfoFromInlineStyle(editor: EditorRenderResult) { | ||
const { jsxMetadata, projectContents, elementPathTree } = editor.getEditorState().editor | ||
|
||
const styleInfoReader = InlineStylePlugin.styleInfoFactory({ | ||
metadata: jsxMetadata, | ||
projectContents: projectContents, | ||
elementPathTree: elementPathTree, | ||
}) | ||
const styleInfo = styleInfoReader(EP.fromString('sb/scene/div')) | ||
return styleInfo | ||
} |
69 changes: 46 additions & 23 deletions
69
editor/src/components/canvas/plugins/inline-style-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters