(variant: string, designSystem: DesignSystem)
| 660 | } |
| 661 | |
| 662 | export function parseVariant(variant: string, designSystem: DesignSystem): Variant | null { |
| 663 | // Arbitrary variants |
| 664 | if (variant[0] === '[' && variant[variant.length - 1] === ']') { |
| 665 | /** |
| 666 | * TODO: Breaking change |
| 667 | * |
| 668 | * @deprecated Arbitrary variants containing at-rules with other selectors |
| 669 | * are deprecated. Use stacked variants instead. |
| 670 | * |
| 671 | * Before: |
| 672 | * - `[@media(width>=123px){&:hover}]:` |
| 673 | * |
| 674 | * After: |
| 675 | * - `[@media(width>=123px)]:[&:hover]:` |
| 676 | * - `[@media(width>=123px)]:hover:` |
| 677 | */ |
| 678 | if (variant[1] === '@' && variant.includes('&')) return null |
| 679 | |
| 680 | let selector = decodeArbitraryValue(variant.slice(1, -1)) |
| 681 | |
| 682 | // Values can't contain `;` or `}` characters at the top-level. |
| 683 | if (!isValidArbitrary(selector)) return null |
| 684 | |
| 685 | // Empty arbitrary values are invalid. E.g.: `[]:` |
| 686 | // ^^ |
| 687 | if (selector.length === 0 || selector.trim().length === 0) return null |
| 688 | |
| 689 | let relative = selector[0] === '>' || selector[0] === '+' || selector[0] === '~' |
| 690 | |
| 691 | // Ensure `&` is always present by wrapping the selector in `&:is(…)`, |
| 692 | // unless it's a relative selector like `> img`. |
| 693 | // |
| 694 | // E.g.: |
| 695 | // |
| 696 | // - `[p]:flex` |
| 697 | if (!relative && selector[0] !== '@' && !selector.includes('&')) { |
| 698 | selector = `&:is(${selector})` |
| 699 | } |
| 700 | |
| 701 | return { |
| 702 | kind: 'arbitrary', |
| 703 | selector, |
| 704 | relative, |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | // Static, functional and compound variants |
| 709 | { |
| 710 | // group-hover/group-name |
| 711 | // ^^^^^^^^^^^ -> Variant without modifier |
| 712 | // ^^^^^^^^^^ -> Modifier |
| 713 | let [variantWithoutModifier, modifier = null, additionalModifier] = segment(variant, '/') |
| 714 | |
| 715 | // If there's more than one modifier, the variant is invalid. |
| 716 | // |
| 717 | // E.g.: |
| 718 | // |
| 719 | // - `group-hover/foo/bar` |
no test coverage detected