Code hacks

by

Anoop

Token-aware colors and fonts

Design tokens keep your site consistent across light and dark themes in Framer. When a user connects a color or shadow variable to a code component, Framer does not pass a string. It passes an object like { "value": "#000000" }. If your component expects a plain string, styles can break or silently reset.

Making your component token aware fixes this. It lets you accept both direct values and connected tokens without any extra setup.

Why it matters

Teams use variables for themes and brand systems. If your component can read token objects correctly, it will match theme changes instantly and behave like Framer’s built-in components. If not, colors and shadows can fail in real projects.

Example

// @framerSupportedLayoutWidth any-prefer-fixed
// @framerSupportedLayoutHeight fixed

import * as React from 'react'
import { addPropertyControls, ControlType } from 'framer'

// Keep types minimal for CMS safety
type Fontish = any

// Safely unwrap tokens like { value: '#000' }
const parseToken = (v: any) => (v && typeof v === 'object' && 'value' in v ? v.value : v)

// Normalize extended font control into CSS
const applyFont = (font?: Fontish): React.CSSProperties => {
  if (!font) return {}
  const lh = typeof font.lineHeight === 'number' ? String(font.lineHeight) + 'px' : font.lineHeight
  return {
    // spread first so we can override safely below
    ...font,
    fontSize: font.fontSize != null ? font.fontSize : font.size,
    lineHeight: lh,
    fontVariantNumeric: 'tabular-nums',
  }
}

type Props = {
  label?: string
  bg?: any
  color?: any
  shadow?: any
  font?: Fontish
}

export default function Button({ label = 'Click me', bg, color, shadow, font }: Props) {
  const fontStyle = applyFont(font)

  return (
    <button style="{{" background:="" parsetoken(bg)="" ||="" '#007aff',="" color:="" parsetoken(color)="" '#ffffff',="" boxshadow:="" parsetoken(shadow)="" '0="" 4px="" 12px="" rgba(0,0,0,0.1)',="" border:="" 'none',="" borderradius:="" 8,="" padding:="" '12px="" 20px',="" cursor:="" 'pointer',="" ...fontstyle,="" }}="">
      {label}
    </button>
  )
}

addPropertyControls(Button, {
  label: { type: ControlType.String, title: 'Label', defaultValue: 'Click me' },
  bg: { type: ControlType.Color, title: 'Background', defaultValue: '#007AFF' },
  color: { type: ControlType.Color, title: 'Text', defaultValue: '#ffffff' },
  shadow: {
    type: ControlType.BoxShadow,
    title: 'Shadow',
    defaultValue: '0 4px 12px rgba(0,0,0,0.1)',
  },
  // @ts-ignore
  font: {
    // Extended Font control
    type: ControlType.Font,
    title: 'Font',
    controls: 'extended',
    displayFontSize: true,
    displayTextAlignment: false,
    defaultValue: {
      fontFamily: 'Inter',
      fontSize: 16,
      lineHeight: 24,
      fontWeight: 500,
    },
  },
})

This uses a tiny parseToken helper to unwrap { value: ... } for colors and shadows. It also includes applyFont to handle extended font controls correctly.

What about fonts

Framer does not let you connect text style tokens to code components yet. You should still handle extended font controls safely. Read font.fontSize first, normalize lineHeight, and use tabular numerals for stable numbers. That keeps typography predictable and prepares your components for future text style variables.

Pro tip

Keep parseToken in a shared utils file and use it across all your components. Apply it anywhere you read colors, shadows, or gradients. Once you add this, your components become theme safe by default.

Code components

Beautiful video player

Free

Beautiful video player

Free

Beautiful video player

Free

HLS video player

$19

HLS video player

$19

HLS video player

$19

Dot Matrix - SVG

Free

Dot Matrix - SVG

Free

Dot Matrix - SVG

Free

FAQs accordion

Free

FAQs accordion

Free

FAQs accordion

Free

Launch count down timer

Free

Launch count down timer

Free

Launch count down timer

Free

View port width

Free

View port width

Free

View port width

Free

Theme switcher

Free

Theme switcher

Free

Theme switcher

Free