-
Notifications
You must be signed in to change notification settings - Fork 343
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
Showing
17 changed files
with
218 additions
and
53 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
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
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,3 +1,4 @@ | ||
export * from './theme' | ||
export * from './types' | ||
export * from './config/default' | ||
export * from './utils/use-component-config-props' |
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,49 @@ | ||
import { computed, watch } from 'vue' | ||
|
||
import { isServer } from '../../../utils/ssr' | ||
import { useGlobalConfig } from '../../../composables/useGlobalConfig' | ||
import { defineGlobalProperty, defineVuesticPlugin } from '../../vue-plugin/utils' | ||
import { addOrUpdateStyleElement } from '../../../utils/dom' | ||
|
||
import { ComponentConfig } from '../types' | ||
import { generateUniqueId } from '../../../utils/uuid' | ||
import { renderComponentsStyles } from '../utils/render-styles-from-config' | ||
|
||
const handleConfigUpdate = (config: Partial<ComponentConfig>, styleId: string) => { | ||
addOrUpdateStyleElement( | ||
`va-component-classes-${styleId}`, | ||
() => renderComponentsStyles(config), | ||
) | ||
} | ||
|
||
const createComponentConfigPlugin = () => { | ||
if (isServer()) { return } | ||
|
||
const { globalConfig } = useGlobalConfig() | ||
|
||
const uniqueId = computed(generateUniqueId) | ||
|
||
watch(() => globalConfig.value.components, (components) => { | ||
if (components) { | ||
handleConfigUpdate(components, uniqueId.value) | ||
} | ||
}, { immediate: true, deep: true }) | ||
|
||
return { | ||
renderStyles: () => { | ||
return renderComponentsStyles(globalConfig.value.components) | ||
}, | ||
} | ||
} | ||
|
||
export const ComponentConfigPlugin = defineVuesticPlugin(() => ({ | ||
install (app) { | ||
defineGlobalProperty(app, '$vaComponentConfig', createComponentConfigPlugin()) | ||
}, | ||
})) | ||
|
||
declare module '@vue/runtime-core' { | ||
export interface ComponentCustomProperties { | ||
$vaComponentConfig: ReturnType<typeof createComponentConfigPlugin> | ||
} | ||
} |
File renamed without changes.
36 changes: 36 additions & 0 deletions
36
packages/ui/src/services/component-config/utils/render-styles-from-config.tsx
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,36 @@ | ||
import { SizesConfig } from '../theme' | ||
import { cssVariableName } from '../../../utils/css-variables' | ||
import { CSSProperties } from 'vue' | ||
import { bemClassName } from '../../../utils/bem' | ||
import { renderCSSRule } from '../../../utils/css' | ||
import { ComponentConfig } from '../types' | ||
|
||
const renderComponentStyles = (sizesConfig: SizesConfig<string, string>, componentName: string, scopeSelector?: string) => { | ||
return Object.entries(sizesConfig).reduce((acc, [size, { variables }]) => { | ||
const definitions: CSSProperties = {} | ||
|
||
for (const property in variables) { | ||
definitions[cssVariableName({ componentName, property })] = variables[property] | ||
} | ||
|
||
const classSelector = `.${bemClassName({ block: componentName, modifier: size })}` | ||
|
||
const selector = scopeSelector ? `${scopeSelector} ${classSelector}` : classSelector | ||
|
||
acc += renderCSSRule(selector, definitions) | ||
|
||
return acc | ||
}, '') | ||
} | ||
|
||
export const renderComponentsStyles = (config: ComponentConfig, scopeSelector?: string) => { | ||
return Object.entries(config).reduce((styles, [componentName, componentConfig]) => { | ||
const sizesConfig: SizesConfig<string, string> | undefined = 'sizesConfig' in componentConfig ? componentConfig.sizesConfig : undefined | ||
|
||
if (sizesConfig) { | ||
styles += renderComponentStyles(sizesConfig, componentName, scopeSelector) | ||
} | ||
|
||
return styles | ||
}, '') | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.