An open source design system that's fully customizable and agent ready
TypeScript
hardening(CommandPalette): layer 1 automated audit fixes (#1030)
Cindy Zhangfd6bcc271117
diff --git a/packages/lab/src/CommandPalette/XDSCommandPalette.tsx b/packages/lab/src/CommandPalette/XDSCommandPalette.tsxindex e455c19f3c..015beddbdc 100644--- a/packages/lab/src/CommandPalette/XDSCommandPalette.tsx+++ b/packages/lab/src/CommandPalette/XDSCommandPalette.tsx@@ -22,6 +22,8 @@ import {} from 'react';import * as stylex from '@stylexjs/stylex';import {XDSDialog} from '@xds/core/Dialog';+import type {XDSBaseProps} from '@xds/core/XDSBaseProps';+import {xdsClassName, mergeProps} from '@xds/core/utils';import {CommandPaletteContext} from './CommandPaletteContext';import {defaultFilter} from './filter';import type {CommandPaletteFilterFn} from './types';@@ -47,7 +49,11 @@ declare module '../theme/types' {}}-export interface XDSCommandPaletteProps {+export interface XDSCommandPaletteProps extends XDSBaseProps<HTMLDivElement> {+ /**+ * Ref forwarded to the root wrapper element inside the dialog.+ */+ ref?: React.Ref<HTMLDivElement>;/*** Whether the command palette is open.*/@@ -161,6 +167,11 @@ export function XDSCommandPalette({width = 640,maxHeight = 480,children,+ ref,+ xstyle,+ className,+ style,+ ...props}: XDSCommandPaletteProps) {const listId = useId();const [search, setSearch] = useState('');@@ -248,7 +259,17 @@ export function XDSCommandPalette({purpose="info"aria-label={label}><CommandPaletteContext.Provider value={contextValue}>- <div {...stylex.props(styles.wrapper)}>{children}</div>+ <div+ ref={ref}+ {...mergeProps(+ xdsClassName('command-palette'),+ stylex.props(styles.wrapper, xstyle),+ className,+ style,+ )}+ {...props}>+ {children}+ </div></CommandPaletteContext.Provider></XDSDialog>);diff --git a/packages/lab/src/CommandPalette/XDSCommandPaletteFooter.tsx b/packages/lab/src/CommandPalette/XDSCommandPaletteFooter.tsxindex 2c5967a162..9b8ecda837 100644--- a/packages/lab/src/CommandPalette/XDSCommandPaletteFooter.tsx+++ b/packages/lab/src/CommandPalette/XDSCommandPaletteFooter.tsx@@ -1,6 +1,6 @@/*** @file XDSCommandPaletteFooter.tsx- * @input Uses React, StyleX, XDSKbd+ * @input Uses React, StyleX, XDSDivider* @output Exports XDSCommandPaletteFooter component* @position Sub-component; footer with keyboard hints*@@ -13,6 +13,8 @@import type {ReactNode} from 'react';import * as stylex from '@stylexjs/stylex';+import type {XDSBaseProps} from '@xds/core/XDSBaseProps';+import {xdsClassName, mergeProps} from '@xds/core/utils';import {colorVars,spacingVars,@@ -40,7 +42,12 @@ const styles = stylex.create({},});-export interface XDSCommandPaletteFooterProps {+export interface XDSCommandPaletteFooterProps extends XDSBaseProps<HTMLDivElement> {+ /**+ * Ref forwarded to the footer element.+ */+ ref?: React.Ref<HTMLDivElement>;+/*** Footer content. When provided, renders custom content instead of default hints.* When omitted, renders default keyboard navigation hints.@@ -67,15 +74,28 @@ export interface XDSCommandPaletteFooterProps {*/export function XDSCommandPaletteFooter({children,+ ref,+ xstyle,+ className,+ style,+ ...props}: XDSCommandPaletteFooterProps) {return (<><XDSDivider />- <div {...stylex.props(styles.footer)}>+ <div+ ref={ref}+ {...mergeProps(+ xdsClassName('command-palette-footer'),+ stylex.props(styles.footer, xstyle),+ className,+ style,+ )}+ {...props}>{children ?? (<>- <span {...stylex.props(styles.hint)}>↑↓ Navigate</span>- <span {...stylex.props(styles.hint)}>↵ Select</span>+ <span {...stylex.props(styles.hint)}>\u2191\u2193 Navigate</span>+ <span {...stylex.props(styles.hint)}>\u21b5 Select</span><span {...stylex.props(styles.hint)}>Esc Close</span></>)}diff --git a/packages/lab/src/CommandPalette/XDSCommandPaletteGroup.tsx b/packages/lab/src/CommandPalette/XDSCommandPaletteGroup.tsxindex 5dc6595490..3da9412fd1 100644--- a/packages/lab/src/CommandPalette/XDSCommandPaletteGroup.tsx+++ b/packages/lab/src/CommandPalette/XDSCommandPaletteGroup.tsx@@ -13,7 +13,8 @@import type {ReactNode} from 'react';import * as stylex from '@stylexjs/stylex';-import type {StyleXStyles} from '@stylexjs/stylex';+import type {XDSBaseProps} from '@xds/core/XDSBaseProps';+import {xdsClassName, mergeProps} from '@xds/core/utils';import {colorVars,spacingVars,@@ -39,7 +40,12 @@ const styles = stylex.create({},});-export interface XDSCommandPaletteGroupProps {+export interface XDSCommandPaletteGroupProps extends XDSBaseProps<HTMLDivElement> {+ /**+ * Ref forwarded to the root element.+ */+ ref?: React.Ref<HTMLDivElement>;+/*** Group heading text.*/@@ -49,11 +55,6 @@ export interface XDSCommandPaletteGroupProps {* Items within this group.*/children: ReactNode;-- /**- * StyleX overrides for the group container.- */- xstyle?: StyleXStyles;}/**@@ -74,13 +75,24 @@ export interface XDSCommandPaletteGroupProps {export function XDSCommandPaletteGroup({heading,children,+ ref,xstyle,+ className,+ style,+ ...props}: XDSCommandPaletteGroupProps) {return (<div+ ref={ref}role="group"aria-label={heading}- {...stylex.props(styles.group, xstyle)}>+ {...mergeProps(+ xdsClassName('command-palette-group'),+ stylex.props(styles.group, xstyle),+ className,+ style,+ )}+ {...props}><div aria-hidden="true" {...stylex.props(styles.heading)}>{heading}</div>diff --git a/packages/lab/src/CommandPalette/XDSCommandPaletteInput.tsx b/packages/lab/src/CommandPalette/XDSCommandPaletteInput.tsxindex ef1b31975e..72bd5fece4 100644--- a/packages/lab/src/CommandPalette/XDSCommandPaletteInput.tsx+++ b/packages/lab/src/CommandPalette/XDSCommandPaletteInput.tsx@@ -12,15 +12,11 @@'use client';-import {- forwardRef,- useCallback,- useEffect,- useRef,- type InputHTMLAttributes,-} from 'react';+import {useCallback, useEffect, useRef, type InputHTMLAttributes} from 'react';import * as stylex from '@stylexjs/stylex';+import type {StyleXStyles} from '@stylexjs/stylex';import {XDSIcon} from '@xds/core/Icon';+import {xdsClassName, mergeProps} from '@xds/core/utils';import {colorVars,typeScaleVars,@@ -64,6 +60,11 @@ export interface XDSCommandPaletteInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>,'type' | 'role' | 'children' | 'autoFocus'> {+ /**+ * Ref forwarded to the input element (for focus management).+ */+ ref?: React.Ref<HTMLInputElement>;+/*** The current search value.* When omitted inside XDSCommandPalette, reads from context.@@ -87,6 +88,11 @@ export interface XDSCommandPaletteInputProps extends Omit<* @default true*/hasAutoFocus?: boolean;++ /**+ * StyleX styles for the wrapper element.+ */+ xstyle?: StyleXStyles;}/**@@ -110,21 +116,17 @@ export interface XDSCommandPaletteInputProps extends Omit<* </XDSCommandPalette>* ```*/-export const XDSCommandPaletteInput = forwardRef<- HTMLInputElement,- XDSCommandPaletteInputProps->(function XDSCommandPaletteInput(- {- value: controlledValue,- onValueChange,- placeholder = 'Search...',- hasAutoFocus = true,- onChange,- onKeyDown,- ...props- },+export function XDSCommandPaletteInput({+ value: controlledValue,+ onValueChange,+ placeholder = 'Search...',+ hasAutoFocus = true,+ onChange,+ onKeyDown,ref,-) {+ xstyle,+ ...props+}: XDSCommandPaletteInputProps) {const ctx = useCommandPaletteContext();const inputRef = useRef<HTMLInputElement>(null);@@ -139,7 +141,8 @@ export const XDSCommandPaletteInput = forwardRef<if (typeof ref === 'function') {ref(element);} else if (ref) {- ref.current = element;+ (ref as React.MutableRefObject<HTMLInputElement | null>).current =+ element;}};@@ -198,7 +201,11 @@ export const XDSCommandPaletteInput = forwardRef<);return (- <div {...stylex.props(styles.wrapper)}>+ <div+ {...mergeProps(+ xdsClassName('command-palette-input'),+ stylex.props(styles.wrapper, xstyle),+ )}><span {...stylex.props(styles.icon)}><XDSIcon icon="search" size="sm" color="inherit" /></span>@@ -228,6 +235,6 @@ export const XDSCommandPaletteInput = forwardRef</></div>);-});+}XDSCommandPaletteInput.displayName = 'XDSCommandPaletteInput';diff --git a/packages/lab/src/CommandPalette/XDSCommandPaletteItem.tsx b/packages/lab/src/CommandPalette/XDSCommandPaletteItem.tsxindex 200885b7af..35a5ddc56c 100644--- a/packages/lab/src/CommandPalette/XDSCommandPaletteItem.tsx+++ b/packages/lab/src/CommandPalette/XDSCommandPaletteItem.tsx@@ -11,15 +11,10 @@'use client';-import {- forwardRef,- useCallback,- useEffect,- useRef,- type ReactNode,-} from 'react';+import {useCallback, useEffect, useRef, type ReactNode} from 'react';import * as stylex from '@stylexjs/stylex';-import type {StyleXStyles} from '@stylexjs/stylex';+import type {XDSBaseProps} from '@xds/core/XDSBaseProps';+import {xdsClassName, mergeProps} from '@xds/core/utils';import {colorVars,spacingVars,@@ -46,7 +41,7 @@ const styles = stylex.create({backgroundColor: 'transparent',border: 'none',cursor: 'pointer',- textAlign: 'left',+ textAlign: 'left' as const,outline: 'none',userSelect: 'none',},@@ -69,7 +64,12 @@ const styles = stylex.create({},});-export interface XDSCommandPaletteItemProps {+export interface XDSCommandPaletteItemProps extends XDSBaseProps<HTMLDivElement> {+ /**+ * Ref forwarded to the root element.+ */+ ref?: React.Ref<HTMLDivElement>;+/*** Unique value for identification and selection.*/@@ -109,11 +109,6 @@ export interface XDSCommandPaletteItemProps {* Item content. Fully custom — render icons, descriptions, shortcuts, etc.*/children: ReactNode;-- /**- * StyleX overrides for the item.- */- xstyle?: StyleXStyles;}/**@@ -133,22 +128,20 @@ export interface XDSCommandPaletteItemProps {* </XDSCommandPaletteItem>* ```*/-export const XDSCommandPaletteItem = forwardRef<- HTMLDivElement,- XDSCommandPaletteItemProps->(function XDSCommandPaletteItem(- {- value,- onSelect,- keywords,- isHighlighted: controlledHighlighted,- isSelected: controlledSelected,- isDisabled = false,- children,- xstyle,- },+export function XDSCommandPaletteItem({+ value,+ onSelect,+ keywords,+ isHighlighted: controlledHighlighted,+ isSelected: controlledSelected,+ isDisabled = false,+ children,ref,-) {+ xstyle,+ className,+ style,+ ...props+}: XDSCommandPaletteItemProps) {const ctx = useCommandPaletteContext();const itemRef = useRef<HTMLDivElement>(null);@@ -159,7 +152,7 @@ export const XDSCommandPaletteItem = forwardRef<if (typeof ref === 'function') {ref(element);} else if (ref) {- ref.current = element;+ (ref as React.MutableRefObject<HTMLDivElement | null>).current = element;}};@@ -214,17 +207,23 @@ export const XDSCommandPaletteItem = forwardRef<data-value={value}onClick={handleClick}onMouseEnter={handleMouseEnter}- {...stylex.props(- styles.item,- !isDisabled && styles.itemHover,- isHighlighted && styles.itemHighlighted,- isSelected && styles.itemSelected,- isDisabled && styles.itemDisabled,- xstyle,- )}>+ {...mergeProps(+ xdsClassName('command-palette-item'),+ stylex.props(+ styles.item,+ !isDisabled && styles.itemHover,+ isHighlighted && styles.itemHighlighted,+ isSelected && styles.itemSelected,+ isDisabled && styles.itemDisabled,+ xstyle,+ ),+ className,+ style,+ )}+ {...props}>{children}</div>);-});+}XDSCommandPaletteItem.displayName = 'XDSCommandPaletteItem';diff --git a/packages/lab/src/CommandPalette/XDSCommandPaletteList.tsx b/packages/lab/src/CommandPalette/XDSCommandPaletteList.tsxindex 5e225a0e05..16e0672389 100644--- a/packages/lab/src/CommandPalette/XDSCommandPaletteList.tsx+++ b/packages/lab/src/CommandPalette/XDSCommandPaletteList.tsx@@ -13,7 +13,8 @@import type {ReactNode} from 'react';import * as stylex from '@stylexjs/stylex';-import type {StyleXStyles} from '@stylexjs/stylex';+import type {XDSBaseProps} from '@xds/core/XDSBaseProps';+import {xdsClassName, mergeProps} from '@xds/core/utils';import {spacingVars} from '@xds/core/theme/tokens.stylex';import {useCommandPaletteContext} from './CommandPaletteContext';@@ -26,7 +27,12 @@ const styles = stylex.create({},});-export interface XDSCommandPaletteListProps {+export interface XDSCommandPaletteListProps extends XDSBaseProps<HTMLDivElement> {+ /**+ * Ref forwarded to the root element.+ */+ ref?: React.Ref<HTMLDivElement>;+/*** Command palette items, groups, empty states, etc.*/@@ -37,11 +43,6 @@ export interface XDSCommandPaletteListProps {* @default 'Commands'*/label?: string;-- /**- * StyleX overrides for the list container.- */- xstyle?: StyleXStyles;}/**@@ -66,16 +67,27 @@ export interface XDSCommandPaletteListProps {export function XDSCommandPaletteList({children,label = 'Commands',+ ref,xstyle,+ className,+ style,+ ...props}: XDSCommandPaletteListProps) {const ctx = useCommandPaletteContext();return (<div+ ref={ref}id={ctx?.listId}role="listbox"aria-label={label}- {...stylex.props(styles.list, xstyle)}>+ {...mergeProps(+ xdsClassName('command-palette-list'),+ stylex.props(styles.list, xstyle),+ className,+ style,+ )}+ {...props}>{children}</div>);