| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | export type ReactProps<T extends HTMLElement> = React.DetailedHTMLProps<React.HTMLAttributes<T>, T>; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | /** |
| b69ab31 | | | 13 | * Like stylex.props(), but also adds in extra classNames. |
| b69ab31 | | | 14 | * Useful since `{...stylex.props()}` sets className, |
| b69ab31 | | | 15 | * and either overwrites or is overwritten by other `className="..."` props. |
| b69ab31 | | | 16 | */ |
| b69ab31 | | | 17 | export function stylexPropsWithClassName( |
| b69ab31 | | | 18 | style: stylex.StyleXStyles, |
| b69ab31 | | | 19 | ...names: Array<string | undefined> |
| b69ab31 | | | 20 | ) { |
| b69ab31 | | | 21 | const {className, ...rest} = stylex.props(style); |
| b69ab31 | | | 22 | return {...rest, className: className + ' ' + names.filter(name => name != null).join(' ')}; |
| b69ab31 | | | 23 | } |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | export function findParentWithClassName( |
| b69ab31 | | | 26 | start: HTMLElement, |
| b69ab31 | | | 27 | className: string, |
| b69ab31 | | | 28 | ): HTMLElement | undefined { |
| b69ab31 | | | 29 | let el = start as HTMLElement | null; |
| b69ab31 | | | 30 | while (el) { |
| b69ab31 | | | 31 | if (el.classList?.contains(className)) { |
| b69ab31 | | | 32 | return el; |
| b69ab31 | | | 33 | } else { |
| b69ab31 | | | 34 | el = el.parentElement; |
| b69ab31 | | | 35 | } |
| b69ab31 | | | 36 | } |
| b69ab31 | | | 37 | return undefined; |
| b69ab31 | | | 38 | } |