-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v5): add Offcanvas component (#5738)
- Loading branch information
Showing
14 changed files
with
1,037 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import PropTypes from 'prop-types'; | ||
import * as React from 'react'; | ||
import { useContext } from 'react'; | ||
import useEventCallback from '@restart/hooks/useEventCallback'; | ||
import CloseButton, { CloseButtonVariant } from './CloseButton'; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
import ModalContext from './ModalContext'; | ||
|
||
export interface AbstractModalHeaderProps | ||
extends React.HTMLAttributes<HTMLDivElement> { | ||
closeLabel?: string; | ||
closeVariant?: CloseButtonVariant; | ||
closeButton?: boolean; | ||
onHide?: () => void; | ||
} | ||
|
||
const propTypes = { | ||
/** | ||
* Provides an accessible label for the close | ||
* button. It is used for Assistive Technology when the label text is not | ||
* readable. | ||
*/ | ||
closeLabel: PropTypes.string, | ||
|
||
/** | ||
* Sets the variant for close button. | ||
*/ | ||
closeVariant: PropTypes.oneOf<CloseButtonVariant>(['white']), | ||
|
||
/** | ||
* Specify whether the Component should contain a close button | ||
*/ | ||
closeButton: PropTypes.bool, | ||
|
||
/** | ||
* A Callback fired when the close button is clicked. If used directly inside | ||
* a ModalContext, the onHide will automatically be propagated up | ||
* to the parent `onHide`. | ||
*/ | ||
onHide: PropTypes.func, | ||
}; | ||
|
||
const defaultProps = { | ||
closeLabel: 'Close', | ||
closeButton: false, | ||
}; | ||
|
||
const AbstractModalHeader = React.forwardRef< | ||
HTMLDivElement, | ||
AbstractModalHeaderProps | ||
>( | ||
( | ||
{ closeLabel, closeVariant, closeButton, onHide, children, ...props }, | ||
ref, | ||
) => { | ||
const context = useContext(ModalContext); | ||
|
||
const handleClick = useEventCallback(() => { | ||
context?.onHide(); | ||
onHide?.(); | ||
}); | ||
|
||
return ( | ||
<div ref={ref} {...props}> | ||
{children} | ||
|
||
{closeButton && ( | ||
<CloseButton | ||
aria-label={closeLabel} | ||
variant={closeVariant} | ||
onClick={handleClick} | ||
/> | ||
)} | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
AbstractModalHeader.propTypes = propTypes; | ||
AbstractModalHeader.defaultProps = defaultProps; | ||
|
||
export default AbstractModalHeader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
good.