Plugin Hooks Reference
PikaCSS provides 10 hooks that plugins can use to interact with the engine at different stages.
Hook Lifecycle
Configuration Phase Hooks
configureRawConfig
Type: async
Modify the raw configuration before it's processed.
async configureRawConfig(config) {
// Add default shortcuts
return {
...config,
shortcuts: [
...(config.shortcuts || []),
['my-shortcut', { color: 'red' }]
]
}
}rawConfigConfigured
Type: sync
Called after raw config is configured. Read-only access.
rawConfigConfigured(config) {
// Log or validate config
console.log('Config loaded:', config)
}configureResolvedConfig
Type: async
Modify the resolved (normalized) configuration.
async configureResolvedConfig(resolvedConfig) {
return {
...resolvedConfig,
prefix: resolvedConfig.prefix || 'pk-'
}
}Setup Phase Hooks
configureEngine
Type: async
Configure the engine instance. This is where you add shortcuts, selectors, variables, etc.
async configureEngine(engine) {
// Add shortcuts
engine.shortcuts.add(
['btn', { padding: '10px', borderRadius: '4px' }]
)
// Add selectors
engine.selectors.add(
['@dark', 'html.dark $']
)
// Add variables
engine.variables.add({
'--primary': '#007bff'
})
// Add keyframes
engine.keyframes.add(
['fadeIn', { from: { opacity: 0 }, to: { opacity: 1 } }]
)
// Add preflight
engine.addPreflight({
':root': { '--plugin-var': 'value' }
})
// Register for autocomplete
engine.appendAutocompleteStyleItemStrings('btn')
engine.appendAutocompleteSelectors('@dark')
}Transform Phase Hooks
transformSelectors
Type: async
Transform selector strings before they're resolved.
async transformSelectors(selectors) {
return selectors.map(selector => {
// Transform @hover to $:hover
if (selector === '@hover') {
return '$:hover'
}
return selector
})
}transformStyleItems
Type: async
Transform style items before they're resolved into style definitions.
async transformStyleItems(styleItems) {
return styleItems.map(item => {
if (typeof item === 'string' && item.startsWith('icon-')) {
// Transform icon shorthand to style object
return { '--icon': item.slice(5) }
}
return item
})
}transformStyleDefinitions
Type: async
Transform resolved style definitions before atomization.
async transformStyleDefinitions(defs) {
return defs.map(def => {
// Transform custom 'size' property
if ('size' in def) {
const { size, ...rest } = def
return {
...rest,
width: size,
height: size
}
}
return def
})
}Event Phase Hooks
preflightUpdated
Type: sync
Called when preflight styles are updated.
preflightUpdated() {
console.log('Preflight styles were updated')
}atomicStyleAdded
Type: sync
Called when a new atomic style is added to the engine. The hook receives the AtomicStyle object containing:
atomicStyleAdded(atomicStyle) {
// atomicStyle: { id: string, content: StyleContent }
console.log('New style:', {
id: atomicStyle.id,
property: atomicStyle.content.property,
value: atomicStyle.content.value,
selector: atomicStyle.content.selector
})
}Parameters:
atomicStyle: The newly added atomic style object withidandcontentproperties
autocompleteConfigUpdated
Type: sync
Called when autocomplete configuration is updated.
autocompleteConfigUpdated() {
console.log('Autocomplete config updated')
}Hook Reference Table
| Hook | Type | Phase | Return |
|---|---|---|---|
configureRawConfig | async | Configuration | EngineConfig | void |
rawConfigConfigured | sync | Configuration | void |
configureResolvedConfig | async | Configuration | ResolvedEngineConfig | void |
configureEngine | async | Setup | void |
transformSelectors | async | Transform | string[] |
transformStyleItems | async | Transform | ResolvedStyleItem[] |
transformStyleDefinitions | async | Transform | ResolvedStyleDefinition[] |
preflightUpdated | sync | Event | void |
atomicStyleAdded | sync | Event | void |
autocompleteConfigUpdated | sync | Event | void |
Engine Utility Methods
Available in configureEngine hook:
Preflight Methods
engine.addPreflight(preflight) // Add preflight styles
engine.notifyPreflightUpdated() // Trigger preflightUpdated hookAutocomplete Methods
engine.appendAutocompleteSelectors(...selectors)
engine.appendAutocompleteStyleItemStrings(...strings)
engine.appendAutocompleteExtraProperties(...properties)
engine.appendAutocompleteExtraCssProperties(...properties)
engine.appendAutocompletePropertyValues(property, ...tsTypes)
engine.appendAutocompleteCssPropertyValues(property, ...values)
engine.notifyAutocompleteConfigUpdated()Notification Methods
engine.notifyPreflightUpdated()
engine.notifyAtomicStyleAdded(atomicStyle)
engine.notifyAutocompleteConfigUpdated()Store Access
engine.store.atomicStyleIds // Map<string, string> - Content hash → ID
engine.store.atomicStyles // Map<string, AtomicStyle> - ID → AtomicStyle