forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageSection.tsx
More file actions
146 lines (137 loc) · 5.34 KB
/
PageSection.tsx
File metadata and controls
146 lines (137 loc) · 5.34 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { useContext, useEffect } from 'react';
import styles from '@patternfly/react-styles/css/components/Page/page';
import { css } from '@patternfly/react-styles';
import { formatBreakpointMods } from '../../helpers/util';
import { PageContext } from './PageContext';
import { PageBody } from './PageBody';
export enum PageSectionVariants {
default = 'default',
secondary = 'secondary'
}
export enum PageSectionTypes {
default = 'default',
subNav = 'subnav',
breadcrumb = 'breadcrumb',
tabs = 'tabs',
wizard = 'wizard'
}
export interface PageSectionProps extends React.HTMLProps<HTMLDivElement> {
/** Content rendered inside the section */
children?: React.ReactNode;
/** Additional classes added to the section */
className?: string;
/** Section background color variant. This will only apply when the type prop has the "default" value. */
variant?: 'default' | 'secondary';
/** Section type variant */
type?: 'default' | 'subnav' | 'breadcrumb' | 'tabs' | 'wizard';
/** Enables the page section to fill the available vertical space if true, or disable filling if false. */
isFilled?: boolean;
/** Limits the width of the section */
isWidthLimited?: boolean;
/** Flag indicating if the section content is center aligned. isWidthLimited must be set for this to work */
isCenterAligned?: boolean;
/** Padding at various breakpoints. */
padding?: {
default?: 'padding' | 'noPadding';
sm?: 'padding' | 'noPadding';
md?: 'padding' | 'noPadding';
lg?: 'padding' | 'noPadding';
xl?: 'padding' | 'noPadding';
'2xl'?: 'padding' | 'noPadding';
};
/** Modifier indicating if the PageBreadcrumb is sticky to the top or bottom at various breakpoints */
stickyOnBreakpoint?: {
default?: 'top' | 'bottom';
sm?: 'top' | 'bottom';
md?: 'top' | 'bottom';
lg?: 'top' | 'bottom';
xl?: 'top' | 'bottom';
'2xl'?: 'top' | 'bottom';
};
/** Modifier indicating if PageSection should have a shadow at the top */
hasShadowTop?: boolean;
/** Modifier indicating if PageSection should have a shadow at the bottom */
hasShadowBottom?: boolean;
/** Flag indicating if the PageSection has a scrolling overflow */
hasOverflowScroll?: boolean;
/** @beta Flag indicating whether children passed to the component should be wrapped by a PageBody.
* Set this to false in order to pass multiple, custom PageBody's as children.
*/
hasBodyWrapper?: boolean;
/** Adds an accessible name to the page section. Required when the hasOverflowScroll prop is set to true.
* This prop should also be passed in if a heading is not being used to describe the content of the page section.
*/
'aria-label'?: string;
/** Sets the base component to render. Defaults to section */
component?: keyof React.JSX.IntrinsicElements;
/** Adds plain styling to the page section. */
isPlain?: boolean;
/** @beta Prevents the page section from automatically applying plain styling when glass theme is enabled. When both this and isPlain are true, isPlain takes precedence. */
isNoPlainOnGlass?: boolean;
}
const variantType = {
[PageSectionTypes.default]: styles.pageMainSection,
[PageSectionTypes.subNav]: styles.pageMainSubnav,
[PageSectionTypes.breadcrumb]: styles.pageMainBreadcrumb,
[PageSectionTypes.tabs]: styles.pageMainTabs,
[PageSectionTypes.wizard]: styles.pageMainWizard
};
const variantStyle = {
[PageSectionVariants.default]: '',
[PageSectionVariants.secondary]: styles.modifiers.secondary
};
export const PageSection: React.FunctionComponent<PageSectionProps> = ({
className = '',
children,
variant = 'default',
type = 'default',
padding,
isFilled,
isWidthLimited = false,
isCenterAligned = false,
stickyOnBreakpoint,
hasShadowTop = false,
hasShadowBottom = false,
hasOverflowScroll = false,
'aria-label': ariaLabel,
component = 'section',
hasBodyWrapper = true,
isPlain = false,
isNoPlainOnGlass = false,
...props
}: PageSectionProps) => {
const { height, getVerticalBreakpoint } = useContext(PageContext);
useEffect(() => {
if (hasOverflowScroll && !ariaLabel) {
/* eslint-disable no-console */
console.warn('PageSection: An accessible aria-label is required when hasOverflowScroll is set to true.');
}
}, [hasOverflowScroll, ariaLabel]);
const Component = component as any;
return (
<Component
{...props}
className={css(
variantType[type],
formatBreakpointMods(padding, styles),
formatBreakpointMods(stickyOnBreakpoint, styles, 'sticky-', getVerticalBreakpoint(height), true),
type === PageSectionTypes.default && variantStyle[variant],
isFilled === false && styles.modifiers.noFill,
isFilled === true && styles.modifiers.fill,
isWidthLimited && styles.modifiers.limitWidth,
isWidthLimited && isCenterAligned && type !== PageSectionTypes.subNav && styles.modifiers.alignCenter,
hasShadowTop && styles.modifiers.shadowTop,
hasShadowBottom && styles.modifiers.shadowBottom,
hasOverflowScroll && styles.modifiers.overflowScroll,
isPlain && styles.modifiers.plain,
isNoPlainOnGlass && styles.modifiers.noPlainOnGlass,
className
)}
{...(hasOverflowScroll && { tabIndex: 0 })}
aria-label={ariaLabel}
>
{hasBodyWrapper ? <PageBody>{children}</PageBody> : children}
</Component>
);
};
PageSection.displayName = 'PageSection';