-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(studio): ui configuration and css styles generation
- Loading branch information
Showing
3 changed files
with
81 additions
and
81 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
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,45 +1,54 @@ | ||
import { kebabCase } from "change-case" | ||
|
||
type StyleConfig = { | ||
base?: Record<string, string> | ||
conditions?: Record<string | number, Record<string, string>> | ||
type CSSProperties = { | ||
[key: string]: string | number | ||
} | ||
|
||
type ColumnnDefinition = | ||
| [string, { style: StyleConfig; formatter?: string }] | ||
| string | ||
type ValueStyles = { | ||
[key: string]: CSSProperties | ||
} | ||
|
||
export function generateCSS(columns: ColumnnDefinition[]) { | ||
let css = "" | ||
type StyleDefinition = CSSProperties | [CSSProperties, ValueStyles] | ||
|
||
const generateCSSProps = (styles: Record<string, string>) => { | ||
let props = "" | ||
for (const [prop, value] of Object.entries(styles)) { | ||
props += `${kebabCase(prop)}: ${value};` | ||
} | ||
return props | ||
} | ||
type Styles = { [key: string]: StyleDefinition } | ||
|
||
export function generateCSS(styles: Styles) { | ||
return Object.entries(styles) | ||
.map(([field, style]) => generateCSSForField(field, style)) | ||
.join("") | ||
} | ||
|
||
for (const column of columns) { | ||
if (typeof column === "string") continue | ||
const generateCSSForField = (field: string, style: StyleDefinition) => { | ||
let fieldCSS = "" | ||
|
||
const prefix = "pinorama-col" | ||
const [className, config] = column | ||
const baseStyles = config.style?.base ?? {} | ||
const conditions = config.style?.conditions ?? {} | ||
const className = `pinorama-${kebabCase(field)}` | ||
|
||
if (Array.isArray(style)) { | ||
const [baseStyles, valueStyles] = style | ||
|
||
// Generate base style | ||
css += `.${prefix}-${kebabCase(className)} {` | ||
css += generateCSSProps(baseStyles) | ||
css += "}\n" | ||
|
||
// Generate conditional style | ||
for (const [condition, conditionStyles] of Object.entries(conditions)) { | ||
css += `.${prefix}-${kebabCase(className)}-${condition} {` | ||
css += generateCSSProps(conditionStyles) | ||
css += "}\n" | ||
fieldCSS += `.${className} {` | ||
fieldCSS += generateCSSProps(baseStyles) | ||
fieldCSS += "}\n" | ||
|
||
// Generate value styles | ||
for (const [value, valueStyle] of Object.entries(valueStyles)) { | ||
fieldCSS += `.${className}-${kebabCase(value)} {` | ||
fieldCSS += generateCSSProps(valueStyle) | ||
fieldCSS += "}\n" | ||
} | ||
} else { | ||
// Generate base style | ||
fieldCSS += `.${className} {` | ||
fieldCSS += generateCSSProps(style) | ||
fieldCSS += "}\n" | ||
} | ||
|
||
return css | ||
return fieldCSS | ||
} | ||
|
||
const generateCSSProps = (style: CSSProperties) => { | ||
return Object.entries(style) | ||
.map(([cssProp, value]) => `${kebabCase(cssProp)}: ${value};`) | ||
.join("") | ||
} |