Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(styles): reworked get-function for tokens #3704

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ body {
}

.docs-story > :first-child {
font-family: tokens.get('post-body', 'font-family');
font-family: tokens.get('post-body-font-family');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR would be a good opportunity to remove the "post-" prefix, don't you think?

Suggested change
font-family: tokens.get('post-body-font-family');
font-family: tokens.get('body-font-family');

}

.font-sans-serif {
Expand All @@ -44,6 +44,6 @@ body {

// full-screen preview
#storybook-root {
font-family: tokens.get('post-body', 'font-family');
font-family: tokens.get('post-body-font-family');
}
}
4 changes: 2 additions & 2 deletions packages/styles/src/components/lead.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
tokens.$default-map: components.$post-lead;

.lead {
font-size: tokens.get('post-lead', 'font-size');
font-weight: tokens.get('post-lead', 'font-weight');
font-size: tokens.get('post-lead-font-size');
font-weight: tokens.get('post-lead-font-weight');
}
12 changes: 6 additions & 6 deletions packages/styles/src/components/text-highlight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
tokens.$default-map: components.$post-text-highlighted;

.text-highlighted {
background-color: tokens.get('post-text', 'highlighted-bg');
color: tokens.get('post-text', 'highlighted-fg');
padding: tokens.get('post-text', 'highlighted-padding-text');
margin-block-start: tokens.get('post-text', 'highlighted-height-space-bottom');
margin-block-end: tokens.get('post-text', 'highlighted-height-space-bottom');
border-radius: tokens.get('post-text', 'highlighted-border-radius');
background-color: tokens.get('post-text-highlighted-bg');
color: tokens.get('post-text-highlighted-fg');
padding: tokens.get('post-text-highlighted-padding-text');
margin-block-start: tokens.get('post-text-highlighted-height-space-bottom');
margin-block-end: tokens.get('post-text-highlighted-height-space-bottom');
border-radius: tokens.get('post-text-highlighted-border-radius');
}
12 changes: 6 additions & 6 deletions packages/styles/src/elements/body.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ tokens.$default-map: elements.$post-body;
@use '../components/fonts';

body {
font-family: tokens.get('post-body', 'font-family');
font-size: tokens.get('post-body', 'font-size');
font-weight: tokens.get('post-body', 'font-weight');
line-height: tokens.get('post-body', 'line-height');
letter-spacing: tokens.get('post-body', 'letter-spacing');
color: tokens.get('post-body', 'color');
font-family: tokens.get('post-body-font-family');
font-size: tokens.get('post-body-font-size');
font-weight: tokens.get('post-body-font-weight');
line-height: tokens.get('post-body-line-height');
letter-spacing: tokens.get('post-body-letter-spacing');
color: tokens.get('post-body-color');
}
40 changes: 20 additions & 20 deletions packages/styles/src/elements/heading.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,48 @@ h5,
.h5,
h6,
.h6 {
line-height: tokens.get('post-heading', 'line-height');
font-weight: tokens.get('post-heading', 'font-weight');
line-height: tokens.get('post-heading-line-height');
font-weight: tokens.get('post-heading-font-weight');
}

h1,
.h1 {
font-size: tokens.get('post-h1', 'font-size');
margin-block-start: tokens.get('post-h1', 'margin-block-start');
margin-block-end: tokens.get('post-h1', 'margin-block-end');
font-size: tokens.get('post-h1-font-size');
margin-block-start: tokens.get('post-h1-margin-block-start');
margin-block-end: tokens.get('post-h1-margin-block-end');
}

h2,
.h2 {
font-size: tokens.get('post-h2', 'font-size');
margin-block-start: tokens.get('post-h2', 'margin-block-start');
margin-block-end: tokens.get('post-h2', 'margin-block-end');
font-size: tokens.get('post-h2-font-size');
margin-block-start: tokens.get('post-h2-margin-block-start');
margin-block-end: tokens.get('post-h2-margin-block-end');
}

h3,
.h3 {
font-size: tokens.get('post-h3', 'font-size');
margin-block-start: tokens.get('post-h3', 'margin-block-start');
margin-block-end: tokens.get('post-h3', 'margin-block-end');
font-size: tokens.get('post-h3-font-size');
margin-block-start: tokens.get('post-h3-margin-block-start');
margin-block-end: tokens.get('post-h3-margin-block-end');
}

h4,
.h4 {
font-size: tokens.get('post-h4', 'font-size');
margin-block-start: tokens.get('post-h4', 'margin-block-start');
margin-block-end: tokens.get('post-h4', 'margin-block-end');
font-size: tokens.get('post-h4-font-size');
margin-block-start: tokens.get('post-h4-margin-block-start');
margin-block-end: tokens.get('post-h4-margin-block-end');
}

h5,
.h5 {
font-size: tokens.get('post-h5', 'font-size');
margin-block-start: tokens.get('post-h5', 'margin-block-start');
margin-block-end: tokens.get('post-h5', 'margin-block-end');
font-size: tokens.get('post-h5-font-size');
margin-block-start: tokens.get('post-h5-margin-block-start');
margin-block-end: tokens.get('post-h5-margin-block-end');
}

h6,
.h6 {
font-size: tokens.get('post-h6', 'font-size');
margin-block-start: tokens.get('post-h6', 'margin-block-start');
margin-block-end: tokens.get('post-h6', 'margin-block-end');
font-size: tokens.get('post-h6-font-size');
margin-block-start: tokens.get('post-h6-margin-block-start');
margin-block-end: tokens.get('post-h6-margin-block-end');
}
75 changes: 27 additions & 48 deletions packages/styles/src/functions/_tokens.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,52 @@
@use 'sass:map';
@use 'sass:string';

$_namespace: 'post';
$default-map: null !default;

/**
* @function get($prefix, $key-name)
* Gets a token-key, normalizes and resolves it with the $default-map.
* @function get($key, $map)
* Gets a token-key, normalizes and resolves it with the given map or $default-map.
*
* @param {string} $prefix - The token map name.
* @param {string} $key-name - The token key name.
* @param {map} $map - The map to get the token key from.
* @param {string} $key - The full token key (including prefix).
* @param {map} $map - The map to get the token key from (optional).
*
* @returns {any} $value of $normalized-prefix[$normalized-key-name]
* @returns {any} $value of $normalized-key in the specified map
*
* @example
* @use '../tokens/components' as components;
* @use '../functions/tokens' as tokens;
*
* // set the default map to use or always use the third function parameter to specify a different map.
* tokens.$default-map: components.$post-body;
*
* // lets say you have two keys: 'post-selector-font-family' and 'post-selector-font-size' in the same map.
* // then use it like so:
* // set the default map to use or always use the second function parameter to specify a different map.
* tokens.$default-map: components.$post-badge;
*
* selector {
* font-family: tokens.get('post-selector', 'font-family');
* font-family: tokens.get('selector', 'font-family');
*
* font-family: tokens.get('selector-font', 'family');
* font-size: tokens.get('selector-font', 'size');
*
* font-family: tokens.get('selector', 'font-family', $my-custom-map); // use a different map than the default one
* background-color: tokens.get('post-badge-bg');
* color: tokens.get('badge-fg');
* border-width: tokens.get('app-store-focus-outline-width', components.$post-app-store-badge); // use a different map than the default one
* }
*/
@function get($key, $map: $default-map) {
$normalized-key: normalize-key($key);

$_namespace: 'post';
$default-map: null !default;

@function get($prefix, $key-name, $map: $default-map) {
$key-name: normalize-key-name($prefix, $key-name);

@if not map.has-key($map, $key-name) {
@error 'No such key "#{$key-name}" in given map! Available keys are: #{meta.inspect(map.keys($map))}.';
@if not meta.type-of($map) == 'map' {
@error 'No map provided or provided $map is not valid. Got #{meta.type-of($map)} instead.';
}

@return map.get($map, $key-name);
}

@function normalize-key-name($original-prefix, $key-name) {
$namespaced-prefix: normalize-prefix($original-prefix);
$prefix: string.slice($namespaced-prefix, string.length('#{$_namespace}-') + 1);
$key-contains-namespace: string.index($key-name, '#{$_namespace}-') == 1;
$key-contains-prefix: string.index($key-name, '#{$prefix}-') == 1;

@if not $key-contains-namespace {
@if not $key-contains-prefix {
$key-name: '#{$namespaced-prefix}-#{$key-name}';
} @else {
$key-name: '#{$_namespace}-#{$key-name}';
}
@if not map.has-key($map, $normalized-key) {
@error 'No such key "#{$normalized-key}" in given map! Available keys are: #{meta.inspect(map.keys($map))}.';
}

@return $key-name;
@return map.get($map, $normalized-key);
}

@function normalize-prefix($prefix) {
$prefix-contains-namespace: string.index($prefix, '#{$_namespace}-') == 1;
@function normalize-key($key) {
$key-parts: string.split($key, '-');
$key-contains-namespace: nth($key-parts, 1) == $_namespace;

@if not $prefix-contains-namespace {
$prefix: '#{$_namespace}-#{$prefix}';
@if not $key-contains-namespace {
$key: '#{$_namespace}-#{$key}';
}

@return $prefix;
}
@return $key;
}
Loading