/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/** * Adds the Mozilla Public License header in one comment and * how to make changes in the generated output files via the * design-tokens.json file in another comment. Also imports * tokens-shared.css when applicable. * * @param {string} surface * Desktop surface, either "brand" or "platform". Determines * whether or not we need to import tokens-shared.css. * @returns {string} Formatted comment header string
*/
let customFileHeader = ({ surface, platform }) => {
let licenseString = [ "/* This Source Code Form is subject to the terms of the Mozilla Public", " * License, v. 2.0. If a copy of the MPL was not distributed with this", " * file, You can obtain one at http://mozilla.org/MPL/2.0/. */",
].join("\n");
let commentString = [ "/* DO NOT EDIT this file directly, instead modify design-tokens.json", " * and run `npm run build` to see your changes. */",
].join("\n");
function formatBaseTokenNames(str) { return str.replaceAll(/(?<tokenName>\w+)-base(?=\b)/g, "$");
}
/** * Creates a surface-specific formatter. The formatter is used to build * our different CSS files, including "prefers-contrast" and "forced-colors" * media queries. See more at * https://amzn.github.io/style-dictionary/#/formats?id=formatter * * @param {string} surface * Which desktop area we are generating CSS for. * Either "brand" (i.e. in-content) or "platform" (i.e. chrome). * @returns {Function} - Formatter function that returns a CSS string.
*/ const createDesktopFormat = surface => args => { return formatBaseTokenNames(
customFileHeader({ surface }) +
formatTokens({
surface,
args,
}) +
formatTokens({
mediaQuery: "prefers-contrast",
surface,
args,
}) +
formatTokens({
mediaQuery: "forced-colors",
surface,
args,
})
);
};
/** * Formats a subset of tokens into CSS. Wraps token CSS in a media query when * applicable. * * @param {object} tokenArgs * @param {string} [tokenArgs.mediaQuery] * Media query formatted CSS should be wrapped in. This is used * to determine what property we are parsing from the token values. * @param {string} [tokenArgs.surface] * Specifies a desktop surface, either "brand" or "platform". * @param {object} tokenArgs.args * Formatter arguments provided by style-dictionary. See more at * https://amzn.github.io/style-dictionary/#/formats?id=formatter * @returns {string} Tokens formatted into a CSS string.
*/ function formatTokens({ mediaQuery, surface, args }) {
let prop = MEDIA_QUERY_PROPERTY_MAP[mediaQuery] ?? "default";
let dictionary = Object.assign({}, args.dictionary);
let tokens = [];
dictionary.allTokens.forEach(token => {
let originalVal = getOriginalTokenValue(token, prop, surface); if (originalVal != undefined) {
let formattedToken = transformTokenValue(token, originalVal, dictionary);
tokens.push(formattedToken);
}
});
/** * Finds the original value of a token for a given media query and surface. * * @param {object} token - Token object parsed by style-dictionary. * @param {string} prop - Name of the property we're querying for. * @param {string} surface * The desktop surface we're generating CSS for, either "brand" or "platform". * @returns {string} The original token value based on our parameters.
*/ function getOriginalTokenValue(token, prop, surface) { if (surface) { return token.original.value[surface]?.[prop];
} elseif (prop == "default" && typeof token.original.value != "object") { return token.original.value;
} return token.original.value?.[prop];
}
/** * Updates a token's value to the relevant original value after resolving * variable references. * * @param {object} token - Token object parsed from JSON by style-dictionary. * @param {string} originalVal * Original value of the token for the combination of surface and media query. * @param {object} dictionary * Object of transformed tokens and helper fns provided by style-dictionary. * @returns {object} Token object with an updated value.
*/ function transformTokenValue(token, originalVal, dictionary) {
let value = originalVal; if (dictionary.usesReference(value)) {
dictionary.getReferences(value).forEach(ref => {
value = value.replace(`{${ref.path.join(".")}}`, `var(--${ref.name})`);
});
} return { ...token, value };
}
/** * Creates a light-dark transform that works for a given surface. Registers * the transform with style-dictionary and returns the transform's name. * * @param {string} surface * The desktop surface we're generating CSS for, either "brand", "platform", * or "shared". * @returns {string} Name of the transform that was registered.
*/ const createLightDarkTransform = surface => {
let name = `lightDarkTransform/${surface}`;
// Matcher function for determining if a token's value needs to undergo // a light-dark transform.
let matcher = token => { if (surface != "shared") { return (
token.original.value[surface]?.light &&
token.original.value[surface]?.dark
);
} return token.original.value.light && token.original.value.dark;
};
// Function that uses the token's original value to create a new "default" // light-dark value and updates the original value object.
let transformer = token => { if (surface != "shared") {
let lightDarkVal = `light-dark(${token.original.value[surface].light}, ${token.original.value[surface].dark})`;
token.original.value[surface].default = lightDarkVal; return token.value;
}
let value = `light-dark(${token.original.value.light}, ${token.original.value.dark})`;
token.original.value.default = value; return value;
};
/** * Format the tokens dictionary to a string. This mostly defers to * StyleDictionary.createPropertyFormatter but first it sorts the tokens based * on the groupings in TOKEN_SECTIONS and adds comment headers to CSS output. * * @param {object} options * Options for tokens to format. * @param {string} options.format * The format to output. Supported: "css" * @param {object} options.dictionary * The tokens dictionary. * @param {string} options.outputReferences * Whether to output variable references. * @param {object} options.formatting * The formatting settings to be passed to createPropertyFormatter. * @returns {string} The formatted tokens.
*/ function formatVariables({ format, dictionary, outputReferences, formatting }) {
let lastSection = [];
let propertyFormatter = createPropertyFormatter({
outputReferences,
dictionary,
format,
formatting,
});
let outputParts = [];
let remainingTokens = [...dictionary.allTokens];
let isFirst = true;
function tokenParts(name) {
let lastDash = name.lastIndexOf("-");
let suffix = name.substring(lastDash + 1); if (TSHIRT_ORDER.includes(suffix) || STATE_ORDER.includes(suffix)) { return [name.substring(0, lastDash), suffix];
} return [name, ""];
}
for (let [label, selector] of Object.entries(TOKEN_SECTIONS)) {
let sectionMatchers = Array.isArray(selector) ? selector : [selector];
let sectionParts = [];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.