addons/components/utils.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import * as stylex from '@stylexjs/stylex';
b69ab319
b69ab3110export type ReactProps<T extends HTMLElement> = React.DetailedHTMLProps<React.HTMLAttributes<T>, T>;
b69ab3111
b69ab3112/**
b69ab3113 * Like stylex.props(), but also adds in extra classNames.
b69ab3114 * Useful since `{...stylex.props()}` sets className,
b69ab3115 * and either overwrites or is overwritten by other `className="..."` props.
b69ab3116 */
b69ab3117export function stylexPropsWithClassName(
b69ab3118 style: stylex.StyleXStyles,
b69ab3119 ...names: Array<string | undefined>
b69ab3120) {
b69ab3121 const {className, ...rest} = stylex.props(style);
b69ab3122 return {...rest, className: className + ' ' + names.filter(name => name != null).join(' ')};
b69ab3123}
b69ab3124
b69ab3125export function findParentWithClassName(
b69ab3126 start: HTMLElement,
b69ab3127 className: string,
b69ab3128): HTMLElement | undefined {
b69ab3129 let el = start as HTMLElement | null;
b69ab3130 while (el) {
b69ab3131 if (el.classList?.contains(className)) {
b69ab3132 return el;
b69ab3133 } else {
b69ab3134 el = el.parentElement;
b69ab3135 }
b69ab3136 }
b69ab3137 return undefined;
b69ab3138}