Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ styled-components >=5.3.0
react-modal >=3.16.1
@emotion/react ^11.13.0
@emotion/styled ^11.13.0
@mui/material ^6.1.4 || ^7.0.0
@mui/icons-material ^6.1.4 || ^7.0.0
@mui/material ^6.4.9 || ^7.0.0
@mui/icons-material ^6.4.9 || ^7.0.0
prop-types ^15.8.1
react-icons ^5.5.0
```
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@
"peerDependencies": {
"@emotion/react": "^11.13.0",
"@emotion/styled": "^11.13.0",
"@mui/icons-material": "^6.1.4 || ^7.0.0",
"@mui/material": "^6.1.4 || ^7.0.0",
"@mui/icons-material": "^6.4.9 || ^7.0.0",
"@mui/material": "^6.4.9 || ^7.0.0",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Toggle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ import { BBBToggle } from 'bbb-ui-components-react';
| `ariaLabel` | `string` | | The accessible name for the toggle. |
| `ariaLabelledBy` | `string` | | The ID of the element that labels the toggle. |
| `ariaDescribedBy` | `string` | | The ID of the element that describes the toggle. |
| `...props` | `SwitchProps` | | Any other props will be passed down to the underlying Material-UI Switch component. |
| `...props` | `Omit<SwitchProps, 'inputProps'>` | | Any other props will be passed down to the underlying Material-UI Switch component. Use `slotProps.input` to set attributes on the `<input>`. |
21 changes: 18 additions & 3 deletions src/components/Toggle/component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { JSX, useId } from 'react';
import { SwitchOwnerState } from '@mui/material';
import { ToggleProps } from './types';
import * as Styled from './styles';
import { DEFAULT_TEXT_POSITION } from './constants';
Expand All @@ -19,13 +20,29 @@ function Toggle ({
ariaLabelledBy,
ariaDescribedBy,
id: providedId,
slotProps,
...rest
}: ToggleProps): JSX.Element {
const generatedUniqueId = useId();
const id = providedId ?? generatedUniqueId;
const labelId = label ? `${id}-label` : undefined;
const helperTextId = helperText ? `${id}-helper` : undefined;

const ariaLabelledByValue = ariaLabel ? undefined : ariaLabelledBy || labelId;
// Without a name source, the <label htmlFor> names the input from the helper text,
// so it can't also be the description.
const hasNameSource = !!ariaLabel || !!ariaLabelledByValue;
const a11yInputProps = {
role: 'switch',
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledByValue,
'aria-describedby': ariaDescribedBy || (hasNameSource ? helperTextId : undefined),
};
Comment thread
Arthurk12 marked this conversation as resolved.
const { input: consumerInputSlotProps, ...otherSlotProps } = slotProps ?? {};
const inputSlotProps = typeof consumerInputSlotProps === 'function'
? (ownerState: SwitchOwnerState) => ({ ...a11yInputProps, ...consumerInputSlotProps(ownerState) })
: { ...a11yInputProps, ...consumerInputSlotProps };

return (
<Styled.ToggleWrapper $textPosition={textPosition}>
{(label || helperText) && (
Expand All @@ -44,9 +61,7 @@ function Toggle ({
{...rest}
id={id}
onChange={onChange}
aria-label={ariaLabel}
aria-labelledby={ariaLabel ? undefined : ariaLabelledBy || labelId}
aria-describedby={ariaDescribedBy || helperTextId}
slotProps={{ ...otherSlotProps, input: inputSlotProps }}
/>
</Styled.ToggleSwitchWrapper>
</Styled.ToggleWrapper>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Toggle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface StyledTextWrapperProps extends StyledToggleWrapperProps{
$hasHelperText: boolean;
}

export interface ToggleProps extends SwitchProps {
export interface ToggleProps extends Omit<SwitchProps, 'inputProps'> {
/** Main label text displayed next to the toggle. */
label?: string;

Expand Down
Loading