Skip to content

Commit

Permalink
organize types
Browse files Browse the repository at this point in the history
  • Loading branch information
liady committed Dec 9, 2024
1 parent 298e9dd commit 2cdfff2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +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
jsxMetadata: ElementInstanceMetadataMap
}) => StyleInfoReader
}

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

export interface InteractionCanvasState {
interactionTarget: InteractionTarget
Expand Down
7 changes: 4 additions & 3 deletions editor/src/components/canvas/plugins/style-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +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?: {
sceneWidth?: number
},
context: StylePluginContext,
) => CSSStyleProperty<NonNullable<ParsedCSSProperties[T]>> | null
updateStyles: (
editorState: EditorState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { extractScreenSizeFromCss } from '../../responsive-utils'
import { TailwindPropertyMapping } from '../tailwind-style-plugin'
import { parseTailwindPropertyFactory } from '../tailwind-style-plugin'
import { getTailwindClassMapping } from '../tailwind-style-plugin'
import type { StylePluginContext } from '../style-plugins'

export const TAILWIND_DEFAULT_SCREENS = {
sm: '640px',
Expand Down Expand Up @@ -82,9 +83,7 @@ export function getPropertiesToAppliedModifiersMap(
currentClassNameAttribute: string,
propertyNames: string[],
config: Config | null,
context: {
sceneWidth?: number
},
context: StylePluginContext,
): Record<string, StyleModifier[]> {
const parseTailwindProperty = parseTailwindPropertyFactory(config, context)
const classMapping = getTailwindClassMapping(currentClassNameAttribute.split(' '), config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { applyValuesAtPath } from '../../commands/utils/property-utils'
import * as PP from '../../../../core/shared/property-path'
import type { Config } from 'tailwindcss/types/config'
import { getPropertiesToAppliedModifiersMap } from './tailwind-responsive-utils'
import type { StylePluginContext } from '../style-plugins'

export type ClassListUpdate =
| { type: 'add'; property: string; value: string }
Expand All @@ -40,9 +41,7 @@ export const runUpdateClassList = (
element: ElementPath,
classNameUpdates: ClassListUpdate[],
config: Config | null,
context: {
sceneWidth?: number
},
context: StylePluginContext,
): EditorStateWithPatch => {
const currentClassNameAttribute =
getClassNameAttribute(getElementFromProjectContents(element, editorState.projectContents))
Expand Down
13 changes: 3 additions & 10 deletions editor/src/components/canvas/plugins/tailwind-style-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getElementFromProjectContents } from '../../editor/store/editor-state'
import type { ParsedCSSProperties } from '../../inspector/common/css-utils'
import { cssParsers } from '../../inspector/common/css-utils'
import { mapDropNulls } from '../../../core/shared/array-utils'
import type { StylePlugin } from './style-plugins'
import type { StylePlugin, StylePluginContext } from './style-plugins'
import type { Config } from 'tailwindcss/types/config'
import type { ParsedVariant, StyleInfo } from '../canvas-types'
import { cssStyleProperty, cssVariant, type CSSStyleProperty } from '../canvas-types'
Expand All @@ -32,12 +32,7 @@ export type TailwindHoverModifier = { type: 'hover'; value: string }
export type TailwindGeneralModifier = TailwindMediaModifier | TailwindHoverModifier

export const parseTailwindPropertyFactory =
(
config: Config | null,
context: {
sceneWidth?: number
},
) =>
(config: Config | null, context: StylePluginContext) =>
<T extends keyof StyleInfo>(
styleDefinition: StyleValueVariants | undefined,
prop: T,
Expand Down Expand Up @@ -173,9 +168,7 @@ export const TailwindPlugin = (config: Config | null): StylePlugin => {
readStyleFromElementProps: <P extends keyof StyleInfo>(
attributes: JSXAttributes,
prop: P,
context?: {
sceneWidth?: number
},
context: StylePluginContext,
): CSSStyleProperty<NonNullable<ParsedCSSProperties[P]>> | null => {
const classNameAttribute = defaultEither(
null,
Expand Down

0 comments on commit 2cdfff2

Please sign in to comment.