Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Separator] Support vertical orientation #1304

Merged
merged 3 commits into from
Jan 8, 2025
Merged
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
5 changes: 5 additions & 0 deletions docs/reference/generated/separator.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"name": "Separator",
"description": "A separator element accessible to screen readers.\nRenders a `<div>` element.",
"props": {
"orientation": {
"type": "'horizontal' | 'vertical'",
"default": "'horizontal'",
"description": "The orientation of the separator."
},
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function ExampleSeparator() {
Support
</a>

<Separator className={styles.Separator} />
<Separator orientation="vertical" className={styles.Separator} />
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are actually vertical but incorrectly defaulted to horizontal before


<a href="#" className={styles.Link}>
Log in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function ExampleSeparator() {
Support
</a>

<Separator className="w-px bg-gray-300" />
<Separator orientation="vertical" className="w-px bg-gray-300" />

<a
href="#"
Expand Down
11 changes: 11 additions & 0 deletions packages/react/src/separator/Separator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ describe('<Separator />', () => {
const { getByRole } = await render(<Separator />);
expect(getByRole('separator')).toBeVisible();
});

describe('prop: orientation', () => {
['horizontal', 'vertical'].forEach((orientation) => {
it(orientation, async () => {
const { getByRole } = await render(
<Separator orientation={orientation as Separator.Props['orientation']} />,
);
expect(getByRole('separator')).to.have.attribute('aria-orientation', orientation);
});
});
});
});
52 changes: 40 additions & 12 deletions packages/react/src/separator/Separator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import type { BaseUIComponentProps } from '../utils/types';
import { mergeReactProps } from '../utils/mergeReactProps';
import { useComponentRenderer } from '../utils/useComponentRenderer';

const EMPTY_OBJECT = {};

/**
* A separator element accessible to screen readers.
* Renders a `<div>` element.
Expand All @@ -16,19 +15,51 @@ const Separator = React.forwardRef(function SeparatorComponent(
props: Separator.Props,
forwardedRef: React.ForwardedRef<HTMLDivElement>,
) {
const { className, render, ...other } = props;
const { className, render, orientation = 'horizontal', ...other } = props;

const state: Separator.State = React.useMemo(() => ({ orientation }), [orientation]);

const getSeparatorProps = React.useCallback(
(externalProps = {}) =>
mergeReactProps(externalProps, {
'aria-orientation': orientation,
}),
[orientation],
);

const { renderElement } = useComponentRenderer({
propGetter: getSeparatorProps,
render: render ?? 'div',
className,
state: EMPTY_OBJECT,
state,
extraProps: { role: 'separator', ...other },
ref: forwardedRef,
});

return renderElement();
});

type Orientation = 'horizontal' | 'vertical';

namespace Separator {
export interface Props extends BaseUIComponentProps<'div', State> {
/**
* The orientation of the separator.
* @default 'horizontal'
*/
orientation?: Orientation;
}

export interface State {
/**
* The orientation of the separator.
*/
orientation: Orientation;
}
}

export { Separator };

Separator.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
Expand All @@ -43,6 +74,11 @@ Separator.propTypes /* remove-proptypes */ = {
* returns a class based on the component’s state.
*/
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* The orientation of the separator.
* @default 'horizontal'
*/
orientation: PropTypes.oneOf(['horizontal', 'vertical']),
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
Expand All @@ -51,11 +87,3 @@ Separator.propTypes /* remove-proptypes */ = {
*/
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
} as any;

namespace Separator {
export interface Props extends BaseUIComponentProps<'div', State> {}

export interface State {}
}

export { Separator };
Loading