-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mauro Erta
committed
Apr 13, 2023
1 parent
2d5470e
commit d692d13
Showing
30 changed files
with
480 additions
and
350 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createUseComponent } from '@morfeo/css'; | ||
|
||
type StyleProps = { | ||
variant: any; | ||
}; | ||
|
||
export const useButton = createUseComponent({ | ||
componentName: 'Button', | ||
py: 'xs', | ||
variant: (props: StyleProps) => props.variant, | ||
}); |
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
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 @@ | ||
export const DYNAMIC_VALUE_TOKEN = 'morfeodynamic'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
theme, | ||
Style, | ||
Property, | ||
ThemeKey, | ||
allPropertiesBySlice, | ||
component, | ||
} from '@morfeo/web'; | ||
import { css } from './css'; | ||
import { getClassesAndCSS } from './getClassesAndCSS'; | ||
|
||
function createDynamicClasses() { | ||
const propertyToSliceCache = new Map<Property, ThemeKey>(); | ||
const pathToStyleCache = new Map<string, Style>(); | ||
|
||
function getThemeSliceByProperty(property: Property) { | ||
if (propertyToSliceCache.has(property)) { | ||
return propertyToSliceCache.get(property) as ThemeKey; | ||
} | ||
|
||
const sliceName = Object.keys(allPropertiesBySlice).find(sliceName => | ||
allPropertiesBySlice[sliceName].includes(property), | ||
) as ThemeKey; | ||
|
||
propertyToSliceCache.set(property, sliceName); | ||
|
||
return sliceName; | ||
} | ||
|
||
function createStyleFromPath(path: string, value: any): Style { | ||
const cacheKey = `${path}-${value}`; | ||
if (pathToStyleCache.has(cacheKey)) { | ||
return pathToStyleCache.get(cacheKey) as Style; | ||
} | ||
|
||
const [first, ...rest] = path.split('.'); | ||
|
||
const style = { | ||
[first]: | ||
rest.length > 0 ? createStyleFromPath(rest.join('.'), value) : value, | ||
}; | ||
|
||
pathToStyleCache.set(cacheKey, style); | ||
|
||
return style; | ||
} | ||
|
||
function createComponentClasses( | ||
property: 'variant' | 'state', | ||
path: string, | ||
completeStyle: Style, | ||
) { | ||
const componentInfo = component(completeStyle.componentName!); | ||
let componentCss = ''; | ||
const variantsOrStates = | ||
componentInfo.get()[property === 'variant' ? 'variants' : 'states']; | ||
|
||
const classes = Object.keys(variantsOrStates).reduce( | ||
(acc, variantOrState) => { | ||
const style = createStyleFromPath(path, variantOrState); | ||
const { classes, css } = getClassesAndCSS({ | ||
className: { | ||
componentName: completeStyle.componentName, | ||
...(completeStyle.state ? { state: completeStyle.state } : {}), | ||
...(completeStyle.variant | ||
? { variant: completeStyle.variant } | ||
: {}), | ||
...style, | ||
}, | ||
}); | ||
|
||
componentCss += css; | ||
|
||
return { | ||
...acc, | ||
[variantOrState]: classes.className, | ||
}; | ||
}, | ||
{}, | ||
); | ||
|
||
return { classes, css: componentCss }; | ||
} | ||
|
||
function create(property: string, path: string, completeStyle: Style) { | ||
if (property === 'variant' || property === 'state') { | ||
return createComponentClasses(property, path, completeStyle); | ||
} | ||
|
||
const sliceName = getThemeSliceByProperty(property as Property); | ||
const slice = theme.getSlice(sliceName); | ||
const sliceKeys = Object.keys(slice); | ||
const classes = sliceKeys.reduce((acc, sliceKey) => { | ||
const style = createStyleFromPath(path, sliceKey); | ||
|
||
return { | ||
...acc, | ||
[sliceKey]: css.add(style), | ||
}; | ||
}, {}); | ||
|
||
return { classes, css: css.get() }; | ||
} | ||
|
||
return { create }; | ||
} | ||
|
||
export const dynamicClasses = createDynamicClasses(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { ObjectExpression } from '@babel/types'; | ||
import generator from '@babel/generator'; | ||
import { escapeString } from '@morfeo/utils'; | ||
import { isThemeableProperty, toJS } from '../utils'; | ||
import { DYNAMIC_VALUE_TOKEN } from '../constants'; | ||
import { Property, theme } from '@morfeo/web'; | ||
|
||
type StyleFunction = { | ||
code: string; | ||
property: string; | ||
variable: string; | ||
}; | ||
|
||
type ThemeableStyleFunction = { | ||
code: string; | ||
path: string; | ||
property: Property; | ||
}; | ||
|
||
export function getStyleObject(node: ObjectExpression) { | ||
const styleFunctions: StyleFunction[] = []; | ||
const themeableStyleFunctions: ThemeableStyleFunction[] = []; | ||
|
||
const styleObject = toJS(node, { | ||
resolveFunction({ path, property, node }) { | ||
const { code } = generator(node, { | ||
compact: true, | ||
}); | ||
const parts = path.split('.'); | ||
const variable = `--${escapeString(path)}`; | ||
const isResponsive = theme.isResponsive({ [property]: '' }); | ||
// In case the values is responsive, the right property is the parent one. | ||
const propertyToCheck = isResponsive ? parts[parts.length - 2] : property; | ||
const isThemeable = isThemeableProperty(propertyToCheck); | ||
|
||
if (isThemeable) { | ||
themeableStyleFunctions.push({ | ||
code, | ||
path, | ||
property: propertyToCheck, | ||
}); | ||
return DYNAMIC_VALUE_TOKEN; | ||
} | ||
|
||
styleFunctions.push({ | ||
code, | ||
property, | ||
variable, | ||
}); | ||
|
||
return `var(${variable})`; | ||
}, | ||
}); | ||
|
||
return { styleObject, styleFunctions, themeableStyleFunctions }; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export * from './css'; | ||
export * from './toJS'; | ||
export * from './splitStyles'; | ||
export * from './dynamicClasses'; | ||
export * from './getStyleObject'; | ||
export * from './getClassesAndCSS'; | ||
export * from './isThemeableProperty'; |
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,7 @@ | ||
import { Property, allProperties } from '@morfeo/web'; | ||
|
||
const componentProperties = ['componentName', 'variant', 'state']; | ||
|
||
export function isThemeableProperty(property: string): property is Property { | ||
return !!allProperties[property] || componentProperties.includes(property); | ||
} |
Oops, something went wrong.