-
Notifications
You must be signed in to change notification settings - Fork 6
Add Modal, and HxDiv elements & Alert event fixes #88
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
61ef94c
Add Modal, and HxDiv elements & Alert event fixes
nicko-winner 630e352
spacing changes
nicko-winner c42b4de
fix prettier
nicko-winner 622bc13
Convert icon to a functional component
nicko-winner b64acfe
Add Everything to default exports
nicko-winner 0f69a97
Code review
nicko-winner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,48 +1,28 @@ | ||
| import React, { useRef, useEffect} from 'react'; | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import useEventListener from '../hooks/useEventListener'; | ||
|
|
||
| const Alert = (props) => { | ||
| const hxRef = useRef(null); | ||
|
|
||
| useEffect(() => { | ||
| hxRef.current.addEventListener('open', props.onOpen); | ||
| hxRef.current.addEventListener('close', props.onClose); | ||
| return () => { | ||
| hxRef.current.removeEventListener('open', props.onOpen); | ||
| hxRef.current.removeEventListener('close', props.onClose); | ||
| }; | ||
| }, []); | ||
|
|
||
| return ( | ||
| <> | ||
| {/* | ||
| Wrappping element needed: Otherwise when alert removes itself from DOM on close it confusing React | ||
| about where highest level parent element went, and will throw an error. | ||
| */} | ||
| <hx-alert | ||
| type={props.type} | ||
| status={props.status} | ||
| persist={props.persist} | ||
| cta={props.cta} | ||
| className={props.className} | ||
| id={props.id} | ||
| ref={hxRef} | ||
| > | ||
| {props.children} | ||
| </hx-alert> | ||
| </> | ||
| ); | ||
| const Alert = ({ onOpen, onClose, className, children, onDismiss, onSubmit, ...rest }) => { | ||
| const hxRef = useEventListener({ onDismiss, onSubmit }); | ||
| return ( | ||
| <> | ||
| {/* Wrappping element needed: Otherwise when alert removes itself from DOM on close, it will cause error */} | ||
| <hx-alert class={className} ref={hxRef} {...rest}> | ||
| {children} | ||
| </hx-alert> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| Alert.propTypes = { | ||
| className: PropTypes.string, | ||
| children: PropTypes.node.isRequired, | ||
| type: PropTypes.string, | ||
| status: PropTypes.string, | ||
| cta: PropTypes.string, | ||
| persist: PropTypes.bool, | ||
| onOpen: PropTypes.func, | ||
| onClose: PropTypes.func | ||
| className: PropTypes.string, | ||
| children: PropTypes.node.isRequired, | ||
| type: PropTypes.string, | ||
| status: PropTypes.string, | ||
| cta: PropTypes.string, | ||
| persist: PropTypes.bool, | ||
| onDismiss: PropTypes.func, | ||
| onSubmit: PropTypes.func, | ||
| }; | ||
|
|
||
| export default Alert; | ||
This file contains hidden or 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
This file contains hidden or 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,14 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const HxDiv = ({ className, ...rest }) => { | ||
| return <hx-div class={className} {...rest} />; | ||
| }; | ||
|
|
||
| HxDiv.propTypes = { | ||
| className: PropTypes.string, | ||
| children: PropTypes.node.isRequired, | ||
| scroll: PropTypes.oneOf(['vertical', 'horizontal', 'both', 'none']), | ||
| }; | ||
|
|
||
| export default HxDiv; |
This file contains hidden or 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
This file contains hidden or 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,32 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import classNames from 'classnames'; | ||
| import useEventListener from '../hooks/useEventListener'; | ||
| import { SIZES } from '../constants'; | ||
| import { wcBool } from '../utils'; | ||
|
|
||
| const Modal = ({ onOpen, onClose, className, open, size, children, ...rest }) => { | ||
| const hxRef = useEventListener({ onOpen, onClose }); | ||
| return ( | ||
| <hx-modal | ||
| class={classNames(className, SIZES[size])} | ||
| ref={hxRef} | ||
| open={wcBool(open)} | ||
| {...rest} | ||
| > | ||
| {children} | ||
| </hx-modal> | ||
| ); | ||
| }; | ||
|
|
||
| Modal.propTypes = { | ||
| id: PropTypes.string, | ||
| size: PropTypes.oneOf(SIZES), | ||
| children: PropTypes.node.isRequired, | ||
| className: PropTypes.string, | ||
| open: PropTypes.bool, | ||
| onClose: PropTypes.func, | ||
| onOpen: PropTypes.func, | ||
| }; | ||
|
|
||
| export default Modal; |
This file contains hidden or 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,67 @@ | ||
| import React from 'react'; | ||
| import { storiesOf } from '@storybook/react'; | ||
| import { action } from '@storybook/addon-actions'; | ||
| import { boolean, select, text } from '@storybook/addon-knobs/react'; | ||
| import Modal from './index'; | ||
| import HxDiv from '../HxDiv'; | ||
| import Button from '../Button'; | ||
|
|
||
| const SIZES = { | ||
| small: 'small', | ||
| medium: 'medium', | ||
| large: 'large', | ||
| }; | ||
|
|
||
| storiesOf('Modal', module).add('All Knobs', () => { | ||
| let header = text('header in H3', 'Modal Header'); | ||
| let footer = text('footer', ''); | ||
| let open = boolean('open', true); | ||
| let size = select('size', SIZES, 'medium'); | ||
| let scroll = boolean('scroll', false); | ||
|
|
||
| const smallText = | ||
| 'This is the body of a demo modal. Interaction with content behind this modal cannot take place until this modal is closed.\n'; | ||
| const lorumIpsum = ( | ||
| <p> | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut | ||
| labore et dolore magna aliqua. Gravida rutrum quisque non tellus. Sagittis vitae et leo duis | ||
| ut diam quam nulla. Diam vel quam elementum pulvinar etiam non. Pulvinar sapien et ligula | ||
| ullamcorper malesuada proin libero nunc. Ultricies integer quis auctor elit sed vulputate mi | ||
| sit amet. Egestas dui id ornare arcu odio ut. In iaculis nunc sed augue. Pellentesque | ||
| adipiscing commodo elit at imperdiet dui accumsan sit amet. Erat velit scelerisque in dictum | ||
| non. Auctor augue mauris augue neque gravida in fermentum et. Posuere sollicitudin aliquam | ||
| ultrices sagittis orci a scelerisque purus. Ullamcorper dignissim cras tincidunt lobortis | ||
| feugiat vivamus at augue. Tincidunt vitae semper quis lectus nulla. Purus ut faucibus pulvinar | ||
| elementum integer enim neque volutpat. Etiam sit amet nisl purus in mollis nunc. Diam sit amet | ||
| nisl suscipit. Nulla pharetra diam sit amet nisl. Arcu odio ut sem nulla. | ||
| </p> | ||
| ); | ||
| const longText = [1, 2, 3, 4, 5].map(() => lorumIpsum); | ||
| const defaultFooter = ( | ||
| <> | ||
| <Button variant="primary">Confirm</Button> | ||
| <Button variant="tertiary">Cancel</Button> | ||
| </> | ||
| ); | ||
|
|
||
| return ( | ||
| <Modal | ||
| {...(open && { open })} | ||
| {...(size && { size })} | ||
| open={open} | ||
| onOpen={action('onOpen')} | ||
| onClose={action('onClose')} | ||
| > | ||
| {header && ( | ||
| <header> | ||
| <h3>{header}</h3> | ||
| </header> | ||
| )} | ||
| <HxDiv scroll={scroll && 'vertical'}> | ||
| {smallText} | ||
| {scroll ? longText : null} | ||
| </HxDiv> | ||
| {<footer>{footer || defaultFooter}</footer>} | ||
| </Modal> | ||
| ); | ||
| }); |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,25 @@ | ||
| import { useEffect, useRef } from 'react'; | ||
|
|
||
| const handlerNameToEvent = (handlerName) => handlerName.replace(/^on/, '').toLowerCase(); | ||
|
|
||
| /** | ||
| * Adds event handlers like onOpen or onClose to event listeners: 'open' or 'close' | ||
| * @param {object} eventHandlers such as onClose, onOpen, ...etc | ||
| * @param {React.MutableRefObject} ref | ||
| * @return {React.MutableRefObject} | ||
| */ | ||
| function useEventListener(eventHandlers = {}, ref) { | ||
| const theRef = ref || useRef(null); | ||
| useEffect(() => { | ||
| Object.entries(eventHandlers).forEach(([handlerName, eventHandler], key) => { | ||
| theRef.current.addEventListener(handlerNameToEvent(handlerName), eventHandler); | ||
| }); | ||
| return () => { | ||
| Object.entries(eventHandlers).forEach(([handlerName, eventHandler], key) => { | ||
| theRef.current.removeEventListener(handlerNameToEvent(handlerName), eventHandler); | ||
| }); | ||
| }; | ||
| }, []); | ||
| return theRef; | ||
| } | ||
| export default useEventListener; |
This file contains hidden or 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
This file contains hidden or 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,7 @@ | ||
| /** | ||
| * Because Web components attributes convert booleans to strings | ||
| * this stops values like false from being converted to "false" and then evaluating as true. | ||
| * @param {boolean} bool | ||
| * @return {*} | ||
| */ | ||
| export const wcBool = (bool) => bool ? true : null; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome idea 👍