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

feat(editor): breakpoint data display (spike, wip) #6690

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export function pickCanvasStateFromEditorState(
propertyControlsInfo: editorState.propertyControlsInfo,
styleInfoReader: activePlugin.styleInfoFactory({
projectContents: editorState.projectContents,
jsxMetadata: editorState.jsxMetadata,
}),
}
}
Expand Down Expand Up @@ -255,6 +256,7 @@ export function pickCanvasStateFromEditorStateWithMetadata(
propertyControlsInfo: editorState.propertyControlsInfo,
styleInfoReader: activePlugin.styleInfoFactory({
projectContents: editorState.projectContents,
jsxMetadata: editorState.jsxMetadata,
}),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ export function controlWithProps<P>(value: ControlWithProps<P>): ControlWithProp

export type StyleInfoReader = (elementPath: ElementPath) => StyleInfo | null

export type StyleInfoFactory = (context: {
export type StyleInfoContext = {
projectContents: ProjectContentTreeRoot
}) => StyleInfoReader
jsxMetadata: ElementInstanceMetadataMap
}

export type StyleInfoFactory = (context: StyleInfoContext) => StyleInfoReader

export interface InteractionCanvasState {
interactionTarget: InteractionTarget
Expand Down
48 changes: 43 additions & 5 deletions editor/src/components/canvas/canvas-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import type {
CSSOverflow,
CSSPadding,
FlexDirection,
ParsedCSSProperties,
} from '../inspector/common/css-utils'
import type { ScreenSize } from './responsive-types'

export const CanvasContainerID = 'canvas-container'
export const SceneContainerName = 'scene'
Expand Down Expand Up @@ -552,9 +554,26 @@ interface CSSStylePropertyNotParsable {

interface ParsedCSSStyleProperty<T> {
type: 'property'
tags: PropertyTag[]
propertyValue: JSExpression | PartOfJSXAttributeValue
value: T
currentVariant: CSSVariant<T>
variants?: CSSVariant<T>[]
}

type StyleModifierMetadata = { type: string; modifierOrigin?: StyleModifierOrigin }
type StyleHoverModifier = StyleModifierMetadata & { type: 'hover' }
export type StyleMediaSizeModifier = StyleModifierMetadata & {
type: 'media-size'
size: ScreenSize
}
export type StyleModifier = StyleHoverModifier | StyleMediaSizeModifier
type InlineModifierOrigin = { type: 'inline' }
type TailwindModifierOrigin = { type: 'tailwind'; variant: string }
export type StyleModifierOrigin = InlineModifierOrigin | TailwindModifierOrigin

export type ParsedVariant<T extends keyof StyleInfo> = {
parsedValue: NonNullable<ParsedCSSProperties[T]>
originalValue: string | number | undefined
modifiers?: StyleModifier[]
}

export type CSSStyleProperty<T> =
Expand All @@ -572,16 +591,35 @@ export function cssStylePropertyNotParsable(
return { type: 'not-parsable', originalValue: originalValue }
}

export type CSSVariant<T> = {
value: T
modifiers?: StyleModifier[]
}

export function cssVariant<T>(value: T, modifiers?: StyleModifier[]): CSSVariant<T> {
return { value: value, modifiers: modifiers }
}

export function cssStyleProperty<T>(
value: T,
propertyValue: JSExpression | PartOfJSXAttributeValue,
currentVariant: CSSVariant<T>,
variants?: CSSVariant<T>[],
): ParsedCSSStyleProperty<T> {
return { type: 'property', tags: [], value: value, propertyValue: propertyValue }
return {
type: 'property',
propertyValue: propertyValue,
currentVariant: currentVariant,
variants: variants ?? [],
}
}

export function screenSizeModifier(size: ScreenSize): StyleMediaSizeModifier {
return { type: 'media-size', size: size }
}

export function maybePropertyValue<T>(property: CSSStyleProperty<T>): T | null {
if (property.type === 'property') {
return property.value
return property.currentVariant.value
}
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const runAdjustCssLengthProperties = (

const styleInfoReader = getActivePlugin(withConflictingPropertiesRemoved).styleInfoFactory({
projectContents: withConflictingPropertiesRemoved.projectContents,
jsxMetadata: withConflictingPropertiesRemoved.jsxMetadata,
})

const styleInfo = styleInfoReader(command.target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const runSetCssLengthProperty = (

const styleInfo = getActivePlugin(editorStateWithPropsDeleted).styleInfoFactory({
projectContents: editorStateWithPropsDeleted.projectContents,
jsxMetadata: editorStateWithPropsDeleted.jsxMetadata,
})(command.target)

if (styleInfo == null) {
Expand Down
4 changes: 2 additions & 2 deletions editor/src/components/canvas/commands/utils/property-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export function getCSSNumberFromStyleInfo(
return { type: 'not-found' }
}

if (prop.type === 'not-parsable' || !isCSSNumber(prop.value)) {
if (prop.type === 'not-parsable' || !isCSSNumber(prop.currentVariant.value)) {
return { type: 'not-css-number' }
}
return { type: 'css-number', number: prop.value }
return { type: 'css-number', number: prop.currentVariant.value }
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const borderRadiusSelector = createCachedSelector(
(store: StyleInfoSubstate) =>
getActivePlugin(store.editor).styleInfoFactory({
projectContents: store.editor.projectContents,
jsxMetadata: store.editor.jsxMetadata,
}),
(_: MetadataSubstate, x: ElementPath) => x,
(metadata, styleInfoReader, selectedElement) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const FlexGapControl = controlForStrategyMemoized<FlexGapControlProps>((p
maybeFlexGapData(
getActivePlugin(store.editor).styleInfoFactory({
projectContents: store.editor.projectContents,
jsxMetadata: store.editor.jsxMetadata,
})(selectedElement),
MetadataUtils.findElementByElementPath(store.editor.jsxMetadata, selectedElement),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ export const PaddingResizeControl = controlForStrategyMemoized((props: PaddingCo
const styleInfoReaderRef = useRefEditorState((store) =>
getActivePlugin(store.editor).styleInfoFactory({
projectContents: store.editor.projectContents,
jsxMetadata: store.editor.jsxMetadata,
}),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ const RemixSceneLabel = React.memo<RemixSceneLabelProps>((props) => {
<div data-testid={RemixSceneLabelTestId(props.target)} style={{ gap: 20 }}>
<span style={{ fontWeight: 600 }}>{scenelabel}</span>{' '}
<span style={{ fontWeight: 400 }}>{sceneSize}</span>
<DeviceSizeButton name='Desktop' sizePx={1024} />
<DeviceSizeButton name='Tablet' sizePx={768} />
<DeviceSizeButton name='Mobile' sizePx={375} />
<DeviceSizeButton name='Reset' sizePx={0} />
</div>
<div
data-testid={RemixSceneLabelPathTestId(props.target)}
Expand Down Expand Up @@ -382,3 +386,20 @@ const RemixSceneLabel = React.memo<RemixSceneLabelProps>((props) => {
</CanvasOffsetWrapper>
)
})

const DeviceSizeButton = React.memo<{ name: string; sizePx: number }>((props) => (
<span
style={{ fontWeight: 400, cursor: 'pointer', marginLeft: 10 }}
onClick={(e) => {
const scene = document.querySelector('[data-testid=remix-scene]')
if (scene != null) {
;(scene as HTMLElement).style.width = props.sizePx === 0 ? '' : `${props.sizePx}px`
}
e.preventDefault()
e.stopPropagation()
}}
onMouseDown={(e) => e.stopPropagation()}
>
{props.name}
</span>
))
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const SubduedFlexGapControl = React.memo<SubduedFlexGapControlProps>((pro
maybeFlexGapData(
getActivePlugin(store.editor).styleInfoFactory({
projectContents: store.editor.projectContents,
jsxMetadata: store.editor.jsxMetadata,
})(selectedElement),
MetadataUtils.findElementByElementPath(store.editor.jsxMetadata, selectedElement),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const SubduedPaddingControl = React.memo<SubduedPaddingControlProps>((pro
const styleInfoReaderRef = useRefEditorState((store) =>
getActivePlugin(store.editor).styleInfoFactory({
projectContents: store.editor.projectContents,
jsxMetadata: store.editor.jsxMetadata,
}),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ export var storyboard = (
const { flexDirection, gap } = styleInfo!
expect(flexDirection).toMatchObject({
type: 'property',
tags: [],
value: 'column',
propertyValue: { value: 'column' },
})
expect(gap).toMatchObject({
type: 'property',
tags: [],
value: cssNumber(2, 'rem'),
propertyValue: { value: '2rem' },
})
Expand Down Expand Up @@ -110,6 +108,7 @@ function getStyleInfoFromInlineStyle(editor: EditorRenderResult) {

const styleInfoReader = InlineStylePlugin.styleInfoFactory({
projectContents: projectContents,
jsxMetadata: jsxMetadata,
})
const styleInfo = styleInfoReader(EP.fromString('sb/scene/div'))
return styleInfo
Expand Down
3 changes: 2 additions & 1 deletion editor/src/components/canvas/plugins/inline-style-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
cssStyleProperty,
cssStylePropertyNotParsable,
cssStylePropertyNotFound,
cssVariant,
} from '../canvas-types'
import { mapDropNulls } from '../../../core/shared/array-utils'
import { emptyComments, jsExpressionValue } from '../../../core/shared/element-template'
Expand Down Expand Up @@ -45,7 +46,7 @@ function getPropertyFromInstance<P extends keyof StyleInfo, T = ParsedCSSPropert
if (Either.isLeft(parsed) || parsed.value == null) {
return cssStylePropertyNotParsable(attribute)
}
return cssStyleProperty(parsed.value, attribute)
return cssStyleProperty(attribute, cssVariant(parsed.value, []))
}

export const InlineStylePlugin: StylePlugin = {
Expand Down
5 changes: 5 additions & 0 deletions editor/src/components/canvas/plugins/style-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ export function deleteCSSProp(property: string): DeleteCSSProp {

export type StyleUpdate = UpdateCSSProp | DeleteCSSProp

export type StylePluginContext = {
sceneWidth?: number
}
export interface StylePlugin {
name: string
styleInfoFactory: StyleInfoFactory
readStyleFromElementProps: <T extends keyof StyleInfo>(
attributes: JSXAttributes,
prop: T,
context: StylePluginContext,
) => CSSStyleProperty<NonNullable<ParsedCSSProperties[T]>> | null
updateStyles: (
editorState: EditorState,
Expand Down Expand Up @@ -248,6 +252,7 @@ export function patchRemovedProperties(editorState: EditorState): EditorState {

const styleInfoReader = activePlugin.styleInfoFactory({
projectContents: editorState.projectContents,
jsxMetadata: editorState.jsxMetadata,
})

const propertiesUpdatedDuringInteraction = getPropertiesUpdatedDuringInteraction(editorState)
Expand Down
Loading
Loading