Skip to content

Commit

Permalink
feat(typography): implement resolve-tokens function to use -text-type
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 509590749
  • Loading branch information
dfreedm authored and copybara-github committed Feb 14, 2023
1 parent 6c2aef6 commit 1550e8e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
31 changes: 31 additions & 0 deletions sass/_typography.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@
@use './theme';
// go/keep-sorted end

/// Resolves a theme's typography tokens for the given prefixes
///
/// @example - scss
/// // $theme has the following tokens:
/// // - label-text-font
/// // - label-text-line-height
/// // - label-text-size
/// // - label-text-tracking
/// // - label-text-weight
/// // - label-text-type
/// @debug resolve-tokens($theme, label-text); // (label-text-type: ...)
///
/// @param {Map} $tokens - Tokens to resolve
/// @param {String...} $token-prefixes - The prefixes of typography tokens to resolve.
/// @return {Map} The resolved tokens
@function resolve-tokens($tokens, $token-prefixes...) {
@each $token-prefix in $token-prefixes {
// leave only `#{$token-prefix}-type`
$tokens: map.remove(
$tokens,
'#{$token-prefix}-font',
'#{$token-prefix}-line-height',
'#{$token-prefix}-size',
'#{$token-prefix}-tracking',
'#{$token-prefix}-weight'
);
}

@return $tokens;
}

/// Resolves a theme's typography tokens for the given prefix.
///
/// @example - scss
Expand Down
56 changes: 56 additions & 0 deletions sass/test/_typography.test.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
//

// go/keep-sorted start
@use 'sass:map';
@use 'sass:meta';
@use 'true' as test;
// go/keep-sorted end
// go/keep-sorted start
@use '../map-ext';
@use '../typography';
// go/keep-sorted end

@include test.describe('typography') {
$input: (
'text-font': 'Roboto',
'text-line-height': 1rem,
'text-size': 1rem,
'text-tracking': 0.005rem,
'text-weight': 400,
'text-type': '400 1rem/1rem Roboto',
);

@include test.describe('resolve-tokens()') {
@include test.it('should return a map') {
$result: typography.resolve-tokens($input);
@include test.assert-equal(meta.type-of($result), 'map');
}

@include test.it('does not modify input without a list of tokens') {
$expected: $input;
$result: typography.resolve-tokens($input);
@include test.assert-equal($expected, $result);
}

@include test.it('should remove typography tokens aside from `-type`') {
$expected: map-ext.pick($input, ('text-type'));
$result: typography.resolve-tokens($input, 'text');
@include test.assert-equal($expected, $result);
}

@include test.it('only modifies specified tokens') {
$input: map.merge(
$input,
(
'other-token': 16px,
)
);
$expected: map-ext.pick($input, ('text-type', 'other-token'));
$result: typography.resolve-tokens($input, 'text');
@include test.assert-equal($expected, $result);
}
}
}
1 change: 1 addition & 0 deletions sass/test/test.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
@use './map-ext.test';
@use './shape.test';
@use './string-ext.test';
@use './typography.test';
// go/keep-sorted end

0 comments on commit 1550e8e

Please sign in to comment.