-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathButtonPanel.tsx
More file actions
123 lines (112 loc) · 3.72 KB
/
Copy pathButtonPanel.tsx
File metadata and controls
123 lines (112 loc) · 3.72 KB
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
import classNames from 'classnames';
import { ButtonVariant, IconArrowLeft, IconMenuDots } from 'hds-react';
import React from 'react';
import { useTranslation } from 'react-i18next';
import Button from '../../../common/components/button/Button';
import MenuDropdown from '../../../common/components/menuDropdown/MenuDropdown';
import Container from '../../../domain/app/layout/container/Container';
import useIsMobile from '../../../hooks/useIsMobile';
import { MenuItemOptionProps } from '../menuDropdown/types';
import styles from './buttonPanel.module.scss';
export interface ButtonPanelProps {
actionItems?: MenuItemOptionProps[];
contentWrapperClassName?: string;
onBack?: () => void;
submitButtons?: React.ReactElement[];
withOffset?: boolean;
}
const SCROLL_OFFSET = 40;
const ButtonPanel: React.FC<ButtonPanelProps> = ({
actionItems,
contentWrapperClassName,
onBack,
submitButtons,
withOffset = true,
}) => {
const buttonPanel = React.useRef<HTMLDivElement>(null);
const { t } = useTranslation();
const isMobile = useIsMobile();
/* istanbul ignore next */
const onDocumentFocusin = (event: FocusEvent) => {
const target = event.target;
if (
target instanceof HTMLElement &&
!buttonPanel.current?.contains(target)
) {
const buttonPanelRect = buttonPanel.current?.getBoundingClientRect();
const targetRect = target.getBoundingClientRect();
if (
buttonPanelRect &&
buttonPanelRect.top < targetRect.bottom &&
window.innerHeight === buttonPanelRect.bottom
) {
window.scrollBy(
0,
targetRect.bottom - buttonPanelRect.top + SCROLL_OFFSET
);
}
}
};
React.useEffect(() => {
document.addEventListener('focusin', onDocumentFocusin);
return () => {
document.removeEventListener('focusin', onDocumentFocusin);
};
});
return (
<div ref={buttonPanel} className={styles.buttonPanel}>
<Container
className={classNames({ [styles.noOffset]: !withOffset })}
withOffset={withOffset}
>
<div className={contentWrapperClassName}>
<div className={styles.buttonsRow}>
<div
className={classNames(styles.submitButtons, {
[styles.hideOnMobile]: !submitButtons?.length,
})}
>
{submitButtons}
</div>
<div
className={classNames(styles.otherButtons, {
[styles.hideOnMobile]: !onBack && !actionItems?.length,
})}
>
{onBack && (
<Button
className={classNames(styles.backButton, styles.smallButton)}
iconStart={<IconArrowLeft aria-hidden />}
fullWidth={true}
onClick={onBack}
type="button"
variant={ButtonVariant.Secondary}
>
{t('common.buttonBack')}
</Button>
)}
<div className={styles.actionsDropdown}>
{!!actionItems?.length && (
<MenuDropdown
button={
isMobile ? (
<button className={styles.toggleButton}>
<IconMenuDots aria-hidden={true} />
</button>
) : undefined
}
buttonLabel={t('common.buttonActions')}
closeOnItemClick={true}
items={actionItems}
menuPosition="top"
/>
)}
</div>
</div>
</div>
</div>
</Container>
</div>
);
};
export default ButtonPanel;