Skip to main content

Overview

Transform hooks allow you to customize how TemPad Dev generates code from Figma designs. Each hook receives specific parameters and returns transformed output.

transform

Transform the generated CSS code and parsed style properties.

Signature

transform?: (params: TransformParams) => string

Parameters

params
TransformParams
required

Returns

code
string
Transformed CSS code string

Example

transform: ({ code, style, options }) => {
  // Add vendor prefixes
  if (style.display === 'flex') {
    return code + ' -webkit-box-orient: vertical;'
  }
  return code
}

transformVariable

Transform CSS variable references to match your theming system.

Signature

transformVariable?: (params: TransformVariableParams) => string

Parameters

params
TransformVariableParams
required

Returns

code
string
Transformed variable reference

Example

// CSS variables to SCSS variables
transformVariable: ({ name }) => {
  return `$${name.replace(/^--/, '')}`
}
// Input:  'var(--color-primary, #6699cc)'
// Output: '$color-primary'

// CSS variables to design tokens
transformVariable: ({ name, value }) => {
  const tokenPath = name.replace(/^--/, '').replace(/-/g, '.')
  return `theme.${tokenPath}`
}
// Input:  'var(--color-text-primary)'
// Output: 'theme.color.text.primary'

transformPx

Transform pixel values to your desired unit system.

Signature

transformPx?: (params: TransformPxParams) => string

Parameters

params
TransformPxParams
required

Returns

value
string
Transformed value with unit

Example

// Convert to rem
transformPx: ({ value, options }) => {
  if (options.useRem) {
    return `${value / options.rootFontSize}rem`
  }
  return `${value}px`
}
// Input:  16
// Output: '1rem' (when useRem is true)

// Custom scaling
transformPx: ({ value }) => {
  return `${value * 0.75}px`
}
// Input:  16
// Output: '12px'

transformComponent

Transform Figma component instances into custom dev components.

Signature

transformComponent?: (params: TransformComponentParams) => DevComponent | string

Parameters

params
TransformComponentParams
required

Returns

result
DevComponent | string
A DevComponent object or raw string markup

Example

import { h } from '@tempad-dev/plugins'

transformComponent: ({ component }) => {
  // Map Figma Button to your design system
  if (component.mainComponent?.name === 'Button') {
    const variant = component.properties.variant || 'primary'
    const label = component.properties.label || 'Button'
    
    return h('Button', { variant }, [label])
  }
  
  // Return null to use default rendering
  return null
}

Using the h() Helper

The h() function creates DevComponent trees with a clean syntax:
import { h } from '@tempad-dev/plugins'

// Simple element
const text = h('Text', 'Hello world')

// With props
const button = h('Button', { variant: 'primary' }, ['Submit'])

// Nested children
const container = h('Container', { size: 'lg' }, [
  h('Heading', { level: 2 }, ['Title']),
  h('Button', { variant: 'primary' }, ['Submit'])
])

Common Patterns

Combining Hooks

export default definePlugin({
  name: 'Custom Theme',
  code: {
    scss: {
      title: 'SCSS',
      lang: 'scss',
      transformVariable: ({ name }) => `$${name.replace(/^--/, '')}`,
      transformPx: ({ value, options }) => 
        options.useRem ? `${value / options.rootFontSize}rem` : `${value}px`,
      transform: ({ code, style }) => {
        // Post-process the entire CSS block
        return code.replace(/px/g, 'rem')
      }
    }
  }
})

Component Mapping

transformComponent: ({ component }) => {
  const componentMap = {
    'Button': (props) => h('UIButton', { ...props }),
    'Input': (props) => h('TextField', { ...props }),
    'Card': (props) => h('Surface', { elevation: 1, ...props })
  }
  
  const transform = componentMap[component.mainComponent?.name]
  return transform ? transform(component.properties) : null
}

See Also

definePlugin

Create a plugin configuration

Types Reference

Full type definitions

Build docs developers (and LLMs) love