MCPcopy Create free account
hub / github.com/adobe/react-spectrum / conditionalToRules

Function conditionalToRules

packages/@react-spectrum/s2/style/style-macro.ts:543–651  ·  view source on GitHub ↗
(
    value: PropertyValueDefinition<P>,
    parentPriority: number,
    currentConditions: Set<string>,
    skipConditions: Set<string>,
    fn: (
      value: P,
      priority: number,
      conditions: Set<string>,
      skipConditions: Set<string>
    ) => [number, Rule[]]
  )

Source from the content-addressed store, hash-verified

541 }
542
543 function conditionalToRules<P extends CustomValue | any[]>(
544 value: PropertyValueDefinition<P>,
545 parentPriority: number,
546 currentConditions: Set<string>,
547 skipConditions: Set<string>,
548 fn: (
549 value: P,
550 priority: number,
551 conditions: Set<string>,
552 skipConditions: Set<string>
553 ) => [number, Rule[]]
554 ): [number, Rule[]] {
555 if (value && typeof value === 'object' && !Array.isArray(value)) {
556 let rules: Rule[] = [];
557
558 // Later conditions in parent rules override conditions in child rules.
559 let subSkipConditions = new Set([...skipConditions, ...Object.keys(value)]);
560
561 // Skip the default condition if we're already filtering by one of the other possible conditions.
562 // For example, if someone specifies `dark: 'gray-400'`, only include the dark version of `gray-400` from the theme.
563 let skipDefault = Object.keys(value).some(k => currentConditions.has(k));
564 let wasCSSCondition = false;
565 let priority = parentPriority;
566
567 for (let condition in value) {
568 if (skipConditions.has(condition) || (condition === 'default' && skipDefault)) {
569 continue;
570 }
571 subSkipConditions.delete(condition);
572
573 let val = value[condition];
574
575 // If a theme condition comes after runtime conditions, create a new grouping.
576 // This makes the CSS class unconditional so it appears outside the `else` block in the JS.
577 // The @layer order in the generated CSS will ensure that it overrides classes applied by runtime conditions.
578 let isCSSCondition = condition in theme.conditions || /^[@:]/.test(condition);
579 if (!wasCSSCondition && isCSSCondition && rules.length) {
580 rules = [new GroupRule(rules)];
581 }
582 wasCSSCondition = isCSSCondition;
583
584 // Increment the current priority whenever we see a new CSS condition.
585 if (isCSSCondition) {
586 priority++;
587 }
588
589 // If this is a runtime condition, inherit the priority from the parent rule.
590 // Otherwise, use the current maximum of the parent and current priorities.
591 let rulePriority = isCSSCondition ? priority : parentPriority;
592
593 if (
594 condition === 'default' ||
595 isCSSCondition ||
596 /^is[A-Z]/.test(condition) ||
597 /^allows[A-Z]/.test(condition)
598 ) {
599 let subConditions = currentConditions;
600 if (isCSSCondition) {

Callers 2

compileValueFunction · 0.70
compileRuleFunction · 0.70

Calls 5

fnFunction · 0.85
deleteMethod · 0.80
pushMethod · 0.80
compileConditionFunction · 0.70
hasMethod · 0.65

Tested by

no test coverage detected