packages/react-aria/docs/separator/useSeparator.mdx
{/* Copyright 2020 Adobe. All rights reserved. This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */}
import {Layout} from '@react-spectrum/docs'; export default Layout;
import docs from 'docs:@react-aria/separator'; import {HeaderInfo, FunctionAPI, TypeContext, InterfaceType, PageDescription} from '@react-spectrum/docs'; import packageData from '@react-aria/separator/package.json';
<PageDescription>{docs.exports.useSeparator.description}</PageDescription>
<HeaderInfo packageData={packageData} componentNames={['useSeparator']} sourceData={[ {type: 'W3C', url: 'https://www.w3.org/TR/wai-aria-1.2/#separator'} ]} />
Horizontal separators can be built with the HTML <hr>
element. However, there is no HTML element for a vertical separator. useSeparator helps achieve
accessible separators that can be styled as needed.
<hr> element or a custom element typeA separator consists of a single element that represents the divider.
useSeparator returns props to be spread onto the element:
<TypeContext.Provider value={docs.links}> <InterfaceType properties={docs.links[docs.exports.useSeparator.return.id].properties} /> </TypeContext.Provider>
This example shows how create both a horizontal and a vertical oriented separator.
import {useSeparator} from '@react-aria/separator';
function Separator(props) {
let {separatorProps} = useSeparator(props);
return (
<div
{...separatorProps}
style={{
background: 'gray',
width: props.orientation === 'vertical' ? '1px' : '100%',
height: props.orientation === 'vertical' ? '100%' : '1px',
margin: props.orientation === 'vertical' ? '0 5px' : '5px 0'
}} />
);
}
<>
<div style={{display: 'flex', flexDirection: 'column'}}>
Content above
<Separator />
Content below
</div>
<div style={{display: 'flex', flexDirection: 'row', marginTop: 20, height: 40, alignItems: 'center'}}>
Content left
<Separator orientation="vertical" />
Content right
</div>
</>