-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathDrawerDemo.tsx
157 lines (142 loc) · 5.18 KB
/
DrawerDemo.tsx
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
import { Component, createRef } from 'react';
import {
Button,
Drawer,
DrawerActions,
DrawerCloseButton,
DrawerContent,
DrawerContentBody,
DrawerHead,
DrawerColorVariant,
DrawerPanelContent,
DrawerProps,
DrawerSection
} from '@patternfly/react-core';
export interface DrawerDemoState {
isExpanded: boolean;
isFocusTrapExpanded: boolean;
isCustomFocusExpanded: boolean;
}
export class DrawerDemo extends Component<DrawerProps, DrawerDemoState> {
static displayName = 'DrawerDemo';
state = {
isExpanded: false,
isFocusTrapExpanded: false,
isCustomFocusExpanded: false
};
drawerRef = createRef<HTMLButtonElement>();
onExpand = () => {
this.drawerRef.current && this.drawerRef.current.focus();
};
onClick = () => {
const isExpanded = !this.state.isExpanded;
this.setState({
isExpanded
});
};
onFocusTrapClick = () => {
this.setState((prevState) => ({ isFocusTrapExpanded: !prevState.isFocusTrapExpanded }));
};
onCustomFocusClick = () => {
this.setState((prevState) => ({ isCustomFocusExpanded: !prevState.isCustomFocusExpanded }));
};
onCloseClick = () => {
this.setState({
isExpanded: false
});
};
onFocusTrapCloseClick = () => {
this.setState({ isFocusTrapExpanded: false });
};
onCustomFocusCloseClick = () => {
this.setState({ isCustomFocusExpanded: false });
};
render() {
const { isExpanded, isFocusTrapExpanded, isCustomFocusExpanded } = this.state;
const panelContent = (
<DrawerPanelContent
widths={{
default: 'width_100',
lg: 'width_50',
xl: 'width_33',
'2xl': 'width_25'
}}
colorVariant={DrawerColorVariant.secondary}
>
<DrawerHead>
<span ref={this.drawerRef} tabIndex={isExpanded ? 0 : -1}>
drawer-panel in demo with onExpand
</span>
<DrawerActions>
<DrawerCloseButton onClick={this.onCloseClick} />
</DrawerActions>
</DrawerHead>
</DrawerPanelContent>
);
const focusTrapPanelContent = (
<DrawerPanelContent
focusTrap={{ enabled: true }}
id="focusTrap-panelContent"
colorVariant={DrawerColorVariant.secondary}
>
<DrawerHead>
<span>drawer-panel</span>
<DrawerActions>
<DrawerCloseButton onClick={this.onFocusTrapCloseClick} />
</DrawerActions>
</DrawerHead>
</DrawerPanelContent>
);
const customFocusPanelContent = (
<DrawerPanelContent
focusTrap={{ enabled: true, elementToFocusOnExpand: '#customFocus-panelContent' }}
id="customFocus-panelContent"
colorVariant={DrawerColorVariant.secondary}
>
<DrawerHead>
<span>drawer-panel</span>
<DrawerActions>
<DrawerCloseButton onClick={this.onCustomFocusCloseClick} />
</DrawerActions>
</DrawerHead>
</DrawerPanelContent>
);
const drawerContent =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pretium est a porttitor vehicula. Quisque vel commodo urna. Morbi mattis rutrum ante, id vehicula ex accumsan ut. Morbi viverra, eros vel porttitor facilisis, eros purus aliquet erat,nec lobortis felis elit pulvinar sem. Vivamus vulputate, risus eget commodo eleifend, eros nibh porta quam, vitae lacinia leo libero at magna. Maecenas aliquam sagittis orci, et posuere nisi ultrices sit amet. Aliquam ex odio, malesuada sed posuere quis, pellentesque at mauris. Phasellus venenatis massa ex, eget pulvinar libero auctor pretium. Aliquam erat volutpat. Duis euismod justo in quam ullamcorper, in commodo massa vulputate.';
return (
<>
<Button id="toggleButton" onClick={this.onClick}>
Toggle Drawer
</Button>
<Drawer
id="basic-drawer"
isExpanded={isExpanded}
onExpand={this.onExpand}
position="bottom"
style={{ minHeight: '300px', height: '300px' }}
>
<DrawerSection colorVariant={DrawerColorVariant.default}>drawer-section</DrawerSection>
<DrawerContent colorVariant={DrawerColorVariant.default} panelContent={panelContent}>
<DrawerContentBody>{drawerContent}</DrawerContentBody>
</DrawerContent>
</Drawer>
<Button id="toggleFocusTrapButton" onClick={this.onFocusTrapClick}>
Toggle Drawer with focus trap
</Button>
<Drawer isExpanded={isFocusTrapExpanded} style={{ minHeight: '300px', height: '300px' }}>
<DrawerContent colorVariant={DrawerColorVariant.default} panelContent={focusTrapPanelContent}>
<DrawerContentBody>{drawerContent}</DrawerContentBody>
</DrawerContent>
</Drawer>
<Button id="toggleCustomFocusButton" onClick={this.onCustomFocusClick}>
Toggle Drawer with custom focus
</Button>
<Drawer isExpanded={isCustomFocusExpanded} style={{ minHeight: '300px', height: '300px' }}>
<DrawerContent colorVariant={DrawerColorVariant.default} panelContent={customFocusPanelContent}>
<DrawerContentBody>{drawerContent}</DrawerContentBody>
</DrawerContent>
</Drawer>
</>
);
}
}