|
| 1 | +/* |
| 2 | + * Copyright 2025 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +interface StyleMacroPropertyDefinition { |
| 14 | + type: 'mapped' | 'percentage' | 'sizing' | 'arbitrary', |
| 15 | + values: string[], |
| 16 | + additionalTypes?: string[] |
| 17 | +} |
| 18 | + |
| 19 | +// Properties that use PercentageProperty (accept LengthPercentage in addition to mapped values) |
| 20 | +const percentageProperties = new Set([ |
| 21 | + 'top', 'left', 'bottom', 'right', |
| 22 | + 'insetStart', 'insetEnd', |
| 23 | + 'marginTop', 'marginBottom', 'marginStart', 'marginEnd', |
| 24 | + 'paddingTop', 'paddingBottom', 'paddingStart', 'paddingEnd', |
| 25 | + 'textIndent', 'translateX', 'translateY' |
| 26 | +]); |
| 27 | + |
| 28 | +// Properties that use SizingProperty (accept number and LengthPercentage in addition to mapped values) |
| 29 | +const sizingProperties = new Set([ |
| 30 | + 'width', 'height', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight', |
| 31 | + 'flexBasis', 'containIntrinsicWidth', 'containIntrinsicHeight' |
| 32 | +]); |
| 33 | + |
| 34 | +// Manually defined property values from spectrum-theme.ts |
| 35 | +// These are extracted from the theme definition |
| 36 | +const propertyValues: {[key: string]: string[]} = { |
| 37 | + display: ['block', 'inline-block', 'inline', 'flex', 'inline-flex', 'grid', 'inline-grid', 'contents', 'list-item', 'none'], |
| 38 | + top: ['0', '2', '4', '8', '12', '16', '20', '24', '28', '32', '36', '40', '44', '48', '56', '64', '80', '96', '-2', '-4', '-8', '-12', '-16', '-20', '-24', '-28', '-32', '-36', '-40', '-44', '-48', '-56', '-64', '-80', '-96', 'auto', 'full'], |
| 39 | + position: ['absolute', 'fixed', 'relative', 'sticky', 'static'], |
| 40 | + width: ['auto', 'full', 'min', 'max', 'fit', 'screen'], |
| 41 | + height: ['auto', 'full', 'min', 'max', 'fit', 'screen'], |
| 42 | + minWidth: ['auto', 'full', 'min', 'max', 'fit', 'screen'], |
| 43 | + minHeight: ['auto', 'full', 'min', 'max', 'fit', 'screen'], |
| 44 | + maxWidth: ['auto', 'full', 'min', 'max', 'fit', 'screen', 'none'], |
| 45 | + maxHeight: ['auto', 'full', 'min', 'max', 'fit', 'screen', 'none'], |
| 46 | + flexDirection: ['row', 'column', 'row-reverse', 'column-reverse'], |
| 47 | + justifyContent: ['normal', 'start', 'end', 'center', 'space-between', 'space-around', 'space-evenly', 'stretch'], |
| 48 | + alignItems: ['start', 'end', 'center', 'baseline', 'stretch'], |
| 49 | + gap: ['0', '2', '4', '8', '12', '16', '20', '24', '28', '32', '36', '40', '44', '48', '56', '64', '80', '96', 'text-to-control', 'text-to-visual', 'edge-to-text', 'pill'] |
| 50 | +}; |
| 51 | + |
| 52 | +export function getPropertyDefinition(propertyName: string): StyleMacroPropertyDefinition { |
| 53 | + const values = propertyValues[propertyName] || []; |
| 54 | + |
| 55 | + if (sizingProperties.has(propertyName)) { |
| 56 | + return { |
| 57 | + type: 'sizing', |
| 58 | + values, |
| 59 | + additionalTypes: ['number', 'LengthPercentage'] |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + if (percentageProperties.has(propertyName)) { |
| 64 | + return { |
| 65 | + type: 'percentage', |
| 66 | + values, |
| 67 | + additionalTypes: ['LengthPercentage'] |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + type: 'mapped', |
| 73 | + values |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +export function getPropertyDefinitions(propertyNames: string[]): {[key: string]: StyleMacroPropertyDefinition} { |
| 78 | + const result: {[key: string]: StyleMacroPropertyDefinition} = {}; |
| 79 | + for (const name of propertyNames) { |
| 80 | + result[name] = getPropertyDefinition(name); |
| 81 | + } |
| 82 | + return result; |
| 83 | +} |
0 commit comments