Skip to content

Commit

Permalink
Extract isElementDisabled util
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Feb 26, 2025
1 parent 3ccf591 commit 4183289
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
7 changes: 3 additions & 4 deletions docs/src/app/(private)/experiments/toolbar/text-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function renderToggleWithTooltip(args: {
export default function App() {
const { settings } = useExperimentSettings<Settings>();

const selectDisabled = settings.toolbarDisabled || settings.selectDisabled;
const SELECT_DISABLED = settings.toolbarDisabled || settings.selectDisabled;
return (
<React.Fragment>
<a
Expand All @@ -141,10 +141,9 @@ export default function App() {
<div className={styles.toolbar.Wrapper}>
<Tooltip.Provider>
<Toolbar.Root className={styles.toolbar.Root}>
{/* only disabling the trigger doesn't work yet - https://github.com/mui/base-ui/issues/1439 */}
<Select.Root defaultValue="sans" disabled={selectDisabled}>
<Select.Root defaultValue="sans">
<Toolbar.Button
disabled={selectDisabled}
disabled={SELECT_DISABLED}
render={<Select.Trigger />}
className={styles.select.Select}
>
Expand Down
11 changes: 3 additions & 8 deletions packages/react/src/accordion/root/useAccordionRoot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';
import * as React from 'react';
import { isElementDisabled } from '../../utils/isElementDisabled';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { useControlled } from '../../utils/useControlled';
import { ARROW_DOWN, ARROW_UP, ARROW_RIGHT, ARROW_LEFT } from '../../composite/composite';
Expand All @@ -15,9 +16,9 @@ function getActiveTriggers(accordionItemRefs: {

for (let i = 0; i < accordionItemElements.length; i += 1) {
const section = accordionItemElements[i];
if (!isDisabled(section)) {
if (!isElementDisabled(section)) {
const trigger = section?.querySelector('[type="button"]') as HTMLButtonElement;
if (!isDisabled(trigger)) {
if (!isElementDisabled(trigger)) {
output.push(trigger);
}
}
Expand All @@ -26,12 +27,6 @@ function getActiveTriggers(accordionItemRefs: {
return output;
}

function isDisabled(element: HTMLElement | null) {
return (
element === null || element.hasAttribute('disabled') || element.hasAttribute('data-disabled')
);
}

export function useAccordionRoot(
parameters: useAccordionRoot.Parameters,
): useAccordionRoot.ReturnValue {
Expand Down
9 changes: 1 addition & 8 deletions packages/react/src/composite/composite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { hasComputedStyleMapSupport } from '../utils/hasComputedStyleMapSupport';
import { isElementDisabled } from '../utils/isElementDisabled';
import { ownerWindow } from '../utils/owner';
import type { TextDirection } from '../direction-provider/DirectionContext';

Expand Down Expand Up @@ -344,14 +345,6 @@ export function getCellIndices(indices: (number | undefined)[], cellMap: (number
return cellMap.flatMap((index, cellIndex) => (indices.includes(index) ? [cellIndex] : []));
}

export function isElementDisabled(element: HTMLElement | null) {
return (
element == null ||
element.hasAttribute('disabled') ||
element.getAttribute('aria-disabled') === 'true'
);
}

export function isDisabled(
list: Array<HTMLElement | null>,
index: number,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/composite/root/useCompositeRoot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';
import * as React from 'react';
import type { TextDirection } from '../../direction-provider/DirectionContext';
import { isElementDisabled } from '../../utils/isElementDisabled';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { useEventCallback } from '../../utils/useEventCallback';
import { useForkRef } from '../../utils/useForkRef';
Expand All @@ -24,7 +25,6 @@ import {
HORIZONTAL_KEYS,
HORIZONTAL_KEYS_WITH_EXTRA_KEYS,
isDisabled,
isElementDisabled,
isIndexOutOfBounds,
isNativeInput,
VERTICAL_KEYS,
Expand Down
9 changes: 2 additions & 7 deletions packages/react/src/select/root/useSelectRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@floating-ui/react';
import { useFieldControlValidation } from '../../field/control/useFieldControlValidation';
import { useFieldRootContext } from '../../field/root/FieldRootContext';
import { isElementDisabled } from '../../utils/isElementDisabled';
import { useBaseUiId } from '../../utils/useBaseUiId';
import { useControlled } from '../../utils/useControlled';
import { type TransitionStatus, useTransitionStatus } from '../../utils';
Expand All @@ -22,12 +23,6 @@ import { useOpenChangeComplete } from '../../utils/useOpenChangeComplete';

const EMPTY_ARRAY: never[] = [];

function isDisabled(element: HTMLElement | null) {
return (
element == null || element.hasAttribute('disabled') || element.hasAttribute('data-disabled')
);
}

export function useSelectRoot<T>(params: useSelectRoot.Parameters<T>): useSelectRoot.ReturnValue {
const {
id: idProp,
Expand Down Expand Up @@ -198,7 +193,7 @@ export function useSelectRoot<T>(params: useSelectRoot.Parameters<T>): useSelect
},
});

const triggerDisabled = isDisabled(triggerElement);
const triggerDisabled = isElementDisabled(triggerElement);

const click = useClick(floatingRootContext, {
enabled: !readOnly && !disabled && !triggerDisabled,
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/utils/isElementDisabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function isElementDisabled(element: HTMLElement | null) {
return (
element == null ||
element.hasAttribute('disabled') ||
element.getAttribute('aria-disabled') === 'true'
);
}

0 comments on commit 4183289

Please sign in to comment.