@wordpress/components
Version:
UI components for WordPress.
8 lines (7 loc) • 1.9 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/utils/space.ts"],
"sourcesContent": ["/**\n * The argument value for the `space()` utility function.\n *\n * When this is a number or a numeric string, it will be interpreted as a\n * multiplier for the grid base value (4px). For example, `space( 2 )` will be 8px.\n *\n * Otherwise, it will be interpreted as a literal CSS length value. For example,\n * `space( 'auto' )` will be 'auto', and `space( '2px' )` will be 2px.\n */\n\nconst GRID_BASE = '4px';\n\n/**\n * A function that handles numbers, numeric strings, and unit values.\n *\n * When given a number or a numeric string, it will return the grid-based\n * value as a factor of GRID_BASE, defined above.\n *\n * When given a unit value or one of the named CSS values like `auto`,\n * it will simply return the value back.\n *\n * @param value A number, numeric string, or a unit value.\n */\nexport function space(value) {\n if (typeof value === 'undefined') {\n return undefined;\n }\n\n // Handle empty strings, if it's the number 0 this still works.\n if (!value) {\n return '0';\n }\n const asInt = typeof value === 'number' ? value : Number(value);\n\n // Test if the input has a unit, was NaN, or was one of the named CSS values (like `auto`), in which case just use that value.\n if (typeof window !== 'undefined' && window.CSS?.supports?.('margin', value.toString()) || Number.isNaN(asInt)) {\n return value.toString();\n }\n return `calc(${GRID_BASE} * ${value})`;\n}"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,IAAM,YAAY;AAaX,SAAS,MAAM,OAAO;AAC3B,MAAI,OAAO,UAAU,aAAa;AAChC,WAAO;AAAA,EACT;AAGA,MAAI,CAAC,OAAO;AACV,WAAO;AAAA,EACT;AACA,QAAM,QAAQ,OAAO,UAAU,WAAW,QAAQ,OAAO,KAAK;AAG9D,MAAI,OAAO,WAAW,eAAe,OAAO,KAAK,WAAW,UAAU,MAAM,SAAS,CAAC,KAAK,OAAO,MAAM,KAAK,GAAG;AAC9G,WAAO,MAAM,SAAS;AAAA,EACxB;AACA,SAAO,QAAQ,SAAS,MAAM,KAAK;AACrC;",
"names": []
}