Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 4x 4x 4x 4x 1x 1x 2x 2x 1x | import React, { Children, useState, useRef, useEffect } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import uniqueId from 'lodash.uniqueid';
import { Popover } from './base-legacy/example';
import { FeedbackHeader } from './error/example';
import { createCustomPropType } from '../../shared/helpers';
import EmptyLink from '../../shared/empty-link/';
/**
* Popover Content - Markup within the popover triggered by the button icon
*/
const PopoverContent = () => (
<p>
The flow has 1 error that must be fixed before you can save.{' '}
<EmptyLink title="Learn More">Learn more.</EmptyLink>
</p>
);
/**
* Button Popover - Button that triggers a popover with focus trapping
*/
const PopoverExperience = ({
activeError,
assistiveText,
children,
isWarning,
isError,
parentCallback,
parentCallbackData,
size,
title,
type
}) => {
/**
* References
*
* buttonIconRef - References the button that triggers the popover
* popoverRef - References popover triggered by button icon
*/
const buttonIconRef = useRef();
const popoverRef = useRef();
/**
* States
*
* alertPopoverOffset - Tracks the position of the popover relative to the button icon that triggered it
* showPopover - Tracks if the popover should be visible or not
* hasKeyboardAccess - Tracks if the popover was triggered by the keyboard
*/
const [alertPopoverOffset, setAlertPopoverOffset] = useState();
const [showPopover, setPopover] = useState(false);
const [hasKeyboardAccess, setKeyboardAccess] = useState(false);
/**
* Handler for click event
*/
const onClick = () => {
showPopover ? onClose() : onOpen();
};
/**
* Handler for opening the popup
*/
const onOpen = () => {
buttonIconRef.current.focus();
setPopover(true);
};
/**
* Handler for closing the popup
*/
const onClose = () => {
buttonIconRef.current.focus();
setPopover(false);
setKeyboardAccess(false);
parentCallback(parentCallbackData);
};
/**
* Handler for button icon losing focus
*/
const onBlur = () => {
if (!hasKeyboardAccess) {
setPopover(false);
parentCallback(parentCallbackData);
}
};
/**
* Handler for Esc key
*/
const onEscKey = () => {
if (showPopover) {
onClose();
}
};
/**
* Focus Trapping
*/
const onTabKey = e => {
if (showPopover) {
setKeyboardAccess(true);
const focusableModalElements = popoverRef.current.querySelectorAll(
'a[href], button, textarea, input[type="text"], input[type="radio"], input[type="checkbox"], select'
);
const firstElement = focusableModalElements[0];
const lastElement =
focusableModalElements[focusableModalElements.length - 1];
if (!e.shiftKey && document.activeElement !== firstElement) {
firstElement.focus();
return e.preventDefault();
}
if (e.shiftKey && document.activeElement !== lastElement) {
lastElement.focus();
e.preventDefault();
}
}
};
/**
* Side Effects - Listen for keystrokes for focus trapping
*/
useEffect(() => {
function keyListener(e) {
const listener = keyListenersMap.get(e.keyCode);
return listener && listener(e);
}
document.addEventListener('keydown', keyListener);
return () => document.removeEventListener('keydown', keyListener);
});
const keyListenersMap = new Map([[27, onEscKey], [9, onTabKey]]);
/**
* Side Effects - Get positioning data corresponding to window
*/
useEffect(() => {
const { current } = buttonIconRef;
const windowResize = () => {
setAlertPopoverOffset({
right: `${document.body.clientWidth -
current.getBoundingClientRect().x -
current.offsetWidth * 1.5 +
6}px`,
top: `${current.getBoundingClientRect().y +
current.offsetHeight +
window.scrollY +
15}px`
});
};
windowResize();
window.addEventListener('resize', windowResize);
return () => document.removeEventListener('resize', windowResize);
}, []);
/**
* Side Effects - Adjust state from external components
*/
useEffect(() => {
if (activeError) {
onOpen();
}
}, [activeError]);
const computedStyles = {
position: 'absolute',
...alertPopoverOffset
};
const headingId = uniqueId('example-unique-id-');
return (
<>
{Children.only(children) &&
React.cloneElement(children, {
size: size,
feedback: type,
symbol: type,
assistiveText: assistiveText,
title: title,
onClick: onClick,
onBlur: onBlur,
innerRef: buttonIconRef
})}
{showPopover &&
ReactDOM.createPortal(
<Popover
isWarning={isWarning}
isError={isError}
headingId={headingId}
style={computedStyles}
onClose={onClose}
header={
<FeedbackHeader
headingId={headingId}
title={`Review ${type}`}
symbol={type}
/>
}
closeButton
iconDefault={type === 'warning'}
inverse={type === 'error'}
innerRef={popoverRef}
nubbinPosition="top right"
>
<PopoverContent />
</Popover>,
document.body
)}
</>
);
};
const activeErrorPropType = createCustomPropType(
false,
(props, propName, componentName) => {
if (!props.isError && props[propName]) {
return new Error('activeError must be used with an error popover');
}
}
);
const childrenPropType = createCustomPropType(
true,
(props, propName, componentName) => {
const validInputs = ['Button', 'ButtonIcon'];
React.Children.forEach(props[propName], child => {
if (!validInputs.includes(child.type.name)) {
throw new Error(
`${componentName} child should be one of the type: ${validInputs.join(
', '
)}`
);
}
});
}
);
PopoverExperience.propTypes = {
activeError: activeErrorPropType,
assistiveText: PropTypes.string,
children: childrenPropType,
isWarning: PropTypes.bool,
isError: PropTypes.bool,
parentCallback: PropTypes.func,
parentCallbackData: PropTypes.node,
size: PropTypes.string,
type: PropTypes.string
};
export default PopoverExperience;
|