Skip to content

Example: Radius + Shape Tokens

This example demonstrates deriving button border-radius tokens from font sizes using a proportional ratio.

The Definitions

math.module.scssdef

scss
---
module: math
title: Math Utilities
summary: General-purpose snapping and rounding functions for token derivation.
category: foundations
since: 0.1.0
tags: [math, rounding, tokens]
---

/// @name snap
/// @summary Snap a value to a step.
/// @param $value <number|dimension|ref> The value to snap.
/// @param $step <number|dimension|ref> The snap interval.
/// @param $mode <nearest|floor|ceil> The rounding mode.
/// @returns <number|dimension>
/// @example snap(15.6px, 1px, nearest) → 16px
/// @example snap(16.86px, 1px, ceil) → 17px
@function snap($value, $step: 1px, $mode: nearest) {
  @return round($value, $step, $mode);
}

radius.module.scssdef

scss
---
module: radius
title: Radius Utilities
summary: Proportional radius derivation functions for component shape tokens.
category: foundations
since: 0.1.0
tags: [radius, shape, tokens]
see: [math]
---

/// @name radius
/// @summary Compute a proportional radius from a size and ratio.
/// @param $size <dimension|ref> Base size (typically a font-size typescale reference).
/// @param $ratio <number|ref> Unitless ratio in the range (0, 1].
/// @param $step <dimension|ref> Snap interval.
/// @param $mode <nearest|floor|ceil> Rounding mode.
/// @returns <dimension>
/// @constraints $ratio > 0
/// @constraints $ratio <= 1
/// @example radius(16px, 0.6, 1px) → 10px
@function radius($size, $ratio, $step: 1px, $mode: nearest) {
  @return snap($size * $ratio, $step, $mode);
}

How It Works

  1. radius() takes a font size and multiplies by a ratio (e.g., 0.6)
  2. The result is snapped to a 1px grid via snap()
  3. snap() delegates to the built-in round() function

Token Output

With shape.ratio.button = 0.6 and typescale base 16px / ratio 1.067:

SizeFont pxShape (radius)Slot-min-size
xs14.058px17px
sm16.0010px20px
md19.4412px24px
lg23.6114px29px
xl28.6817px35px
2xl32.6520px40px
3xl39.6724px48px

DTCG Extension

json
{
  "shape": {
    "button": {
      "md": {
        "$value": "12px",
        "$type": "dimension",
        "$extensions": {
          "org.dtcg-formulas": {
            "formula": "radius({typography.scale.15}, {shape.ratio.button})",
            "definition": "tokens/functions/radius.module.scssdef#radius"
          }
        }
      }
    }
  }
}

Released under the MIT License.