Module: Playbook::Tokens

Defined in:
lib/playbook/tokens.rb

Overview

Playbook design tokens for Rails. One JSON file per token type, generated by scripts/generate-tokens.js (yarn generate:tokens, run from playbook/). Source: token SCSS + types.

--- Current design --- Colors: tokens/colors.json (all keys from _colors.module.scss — legacy + accessible). When you add or change color tokens, run yarn generate:tokens and stage the updated colors.json; otherwise Overcommit will run the generator on commit and fail until you do.

--- Next steps for additional token types ---

  • One JSON per type (tokens/typography.json, tokens/spacing.json, …) and one method per type below.

  • For each new type: add a method (e.g. def typography) that loads the JSON and wraps in ColorHash (or a similar wrapper if the shape differs [TODO: Rename ColorHash to TokenHash if appropriate]). Add a reload_typography! (or reload_*!) if needed.

  • Ensure generate-tokens.js is extended to write that JSON (same pattern: compile SCSS → extract → write).

  • Update .overcommit.yml include paths and .git-hooks/pre_commit/verify_tokens.sh to compare the new generated file so commits stay in sync.

    Example for typography:

    def typography
    @typography ||= ColorHash.new(JSON.parse(File.read(File.join(__dir__, "tokens", "typography.json")), symbolize_names: true))
    end
    def reload_typography!
    @typography = nil; typography
    end

Examples:

Playbook::Tokens.colors[:primary]           # => "#0056CF"
Playbook::Tokens.colors.primary             # => "#0056CF"
Playbook::Tokens.colors[:input_text_error]  # => "#DA0014"

Defined Under Namespace

Classes: ColorHash

Class Method Summary collapse

Class Method Details

.colorsColorHash

Load colors from generated tokens/colors.json (legacy + accessible).

Returns:

  • (ColorHash)

    colors with both hash and method access



41
42
43
44
45
46
47
# File 'lib/playbook/tokens.rb', line 41

def colors
  @colors ||= begin
    json_path = File.join(__dir__, "tokens", "colors.json")
    hash = JSON.parse(File.read(json_path), symbolize_names: true)
    ColorHash.new(hash)
  end
end

.reload_colors!Object

Reload colors from JSON (useful after regenerating)



50
51
52
53
# File 'lib/playbook/tokens.rb', line 50

def reload_colors!
  @colors = nil
  colors
end