|
| 1 | +'use strict'; |
| 2 | +import { webPropsBuilder } from '../../../common/web'; |
| 3 | +import type { ReanimatedHTMLElement } from '../../../ReanimatedModule/js-reanimated'; |
| 4 | +import type { ICSSPseudoSelectorsManager } from '../../types/interfaces'; |
| 5 | +import type { PseudoStylesBySelector } from '../../utils'; |
| 6 | +import { insertPseudoSelectorCSS, removePseudoSelectorCSS } from '../domUtils'; |
| 7 | + |
| 8 | +let pseudoSelectorCounter = 0; |
| 9 | + |
| 10 | +// CSS rules are injected in this order so that later selectors override earlier ones |
| 11 | +// when multiple are active simultaneously. Mirrors web convention (LVHA). |
| 12 | +const SELECTOR_ORDER = [':focus', ':hover', ':active'] as const; |
| 13 | +type KnownSelector = (typeof SELECTOR_ORDER)[number]; |
| 14 | + |
| 15 | +export default class CSSPseudoSelectorsManager |
| 16 | + implements ICSSPseudoSelectorsManager |
| 17 | +{ |
| 18 | + private readonly element: ReanimatedHTMLElement; |
| 19 | + |
| 20 | + private pseudoSelectorClassName: string | null = null; |
| 21 | + private eventListeners: Array<[EventTarget, string, EventListener]> = []; |
| 22 | + |
| 23 | + constructor(element: ReanimatedHTMLElement) { |
| 24 | + this.element = element; |
| 25 | + } |
| 26 | + |
| 27 | + update(pseudoStylesBySelector: PseudoStylesBySelector | null): void { |
| 28 | + this.removeEventListeners(); |
| 29 | + |
| 30 | + if (!pseudoStylesBySelector) { |
| 31 | + this.detach(); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + if (!this.pseudoSelectorClassName) { |
| 36 | + this.pseudoSelectorClassName = `rps-${pseudoSelectorCounter++}`; |
| 37 | + } |
| 38 | + |
| 39 | + const className = this.pseudoSelectorClassName; |
| 40 | + |
| 41 | + const orderedSelectors = SELECTOR_ORDER.filter( |
| 42 | + (sel) => sel in pseudoStylesBySelector |
| 43 | + ); |
| 44 | + |
| 45 | + const rules = orderedSelectors |
| 46 | + .map((selector) => { |
| 47 | + const { selectorStyle } = pseudoStylesBySelector[selector]; |
| 48 | + const css = webPropsBuilder.build(selectorStyle); |
| 49 | + if (!css) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + // Inline styles applied by RNW have higher specificity than class selectors, |
| 53 | + // so !important is required for our state classes to win. |
| 54 | + const cssWithImportant = css |
| 55 | + .split('; ') |
| 56 | + .map((decl) => `${decl} !important`) |
| 57 | + .join('; '); |
| 58 | + return `.${this.stateClass(selector)} { ${cssWithImportant} }`; |
| 59 | + }) |
| 60 | + .filter(Boolean) |
| 61 | + .join('\n'); |
| 62 | + |
| 63 | + insertPseudoSelectorCSS(className, rules); |
| 64 | + |
| 65 | + for (const selector of orderedSelectors) { |
| 66 | + this.attachListenersForSelector(selector); |
| 67 | + } |
| 68 | + |
| 69 | + // When transitionDuration is set but transitionProperty was not explicitly |
| 70 | + // specified, CSSTransitionsManager leaves transitionProperty as an empty |
| 71 | + // string and the browser won't animate anything. Default to 'all'. |
| 72 | + if ( |
| 73 | + !this.element.style.transitionProperty && |
| 74 | + this.element.style.transitionDuration |
| 75 | + ) { |
| 76 | + this.element.style.transitionProperty = 'all'; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + unmountCleanup(): void { |
| 81 | + this.removeEventListeners(); |
| 82 | + this.detach(); |
| 83 | + } |
| 84 | + |
| 85 | + private stateClass(selector: string): string { |
| 86 | + // ':hover' → 'rps-0-hover', ':active' → 'rps-0-active', etc. |
| 87 | + return `${this.pseudoSelectorClassName}${selector.replace(':', '-')}`; |
| 88 | + } |
| 89 | + |
| 90 | + private attachListenersForSelector(selector: KnownSelector): void { |
| 91 | + const cls = this.stateClass(selector); |
| 92 | + const add = () => this.element.classList.add(cls); |
| 93 | + const remove = () => this.element.classList.remove(cls); |
| 94 | + |
| 95 | + switch (selector) { |
| 96 | + case ':hover': |
| 97 | + this.on(this.element, 'mouseenter', add); |
| 98 | + this.on(this.element, 'mouseleave', remove); |
| 99 | + break; |
| 100 | + |
| 101 | + case ':active': |
| 102 | + this.on(this.element, 'mousedown', add); |
| 103 | + this.on(document, 'mouseup', remove); |
| 104 | + break; |
| 105 | + |
| 106 | + case ':focus': { |
| 107 | + // Mirrors native behavior: :focus only fires for text inputs |
| 108 | + const target = this.getFocusTarget(); |
| 109 | + if (target) { |
| 110 | + this.on(target, 'focus', add); |
| 111 | + this.on(target, 'blur', remove); |
| 112 | + } |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private getFocusTarget(): Element | null { |
| 119 | + const isFocusable = (el: Element): boolean => { |
| 120 | + const input = el as HTMLInputElement; |
| 121 | + return !input.disabled && !input.readOnly; |
| 122 | + }; |
| 123 | + |
| 124 | + const tag = this.element.tagName?.toLowerCase(); |
| 125 | + if (tag === 'input' || tag === 'textarea') { |
| 126 | + return isFocusable(this.element) ? this.element : null; |
| 127 | + } |
| 128 | + return this.element.querySelector( |
| 129 | + 'input:not([disabled]):not([readonly]), textarea:not([disabled]):not([readonly])' |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + private on(target: EventTarget, type: string, listener: EventListener): void { |
| 134 | + target.addEventListener(type, listener); |
| 135 | + this.eventListeners.push([target, type, listener]); |
| 136 | + } |
| 137 | + |
| 138 | + private removeEventListeners(): void { |
| 139 | + for (const [target, type, listener] of this.eventListeners) { |
| 140 | + target.removeEventListener(type, listener); |
| 141 | + } |
| 142 | + this.eventListeners = []; |
| 143 | + } |
| 144 | + |
| 145 | + private detach(): void { |
| 146 | + // Remove any state classes that may still be on the element |
| 147 | + if (this.pseudoSelectorClassName) { |
| 148 | + for (const sel of SELECTOR_ORDER) { |
| 149 | + this.element.classList.remove(this.stateClass(sel)); |
| 150 | + } |
| 151 | + removePseudoSelectorCSS(this.pseudoSelectorClassName); |
| 152 | + this.pseudoSelectorClassName = null; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments