Debian Codegraph @

node/@patternfly/react-core/src/components/Page/PageSection.tsx

raw
1 import { useContext, useEffect } from 'react';
2 import styles from '@patternfly/react-styles/css/components/Page/page';
3 import { css } from '@patternfly/react-styles';
4 import { formatBreakpointMods } from '../../helpers/util';
5 import { PageContext } from './PageContext';
6 import { PageBody } from './PageBody';
7
8 export enum PageSectionVariants {
9 default = 'default',
10 secondary = 'secondary'
11 }
12
13 export enum PageSectionTypes {
14 default = 'default',
15 subNav = 'subnav',
16 breadcrumb = 'breadcrumb',
17 tabs = 'tabs',
18 wizard = 'wizard'
19 }
20
21 export interface PageSectionProps extends React.HTMLProps<HTMLDivElement> {
22 /** Content rendered inside the section */
23 children?: React.ReactNode;
24 /** Additional classes added to the section */
25 className?: string;
26 /** Section background color variant. This will only apply when the type prop has the "default" value. */
27 variant?: 'default' | 'secondary';
28 /** Section type variant */
29 type?: 'default' | 'subnav' | 'breadcrumb' | 'tabs' | 'wizard';
30 /** Enables the page section to fill the available vertical space if true, or disable filling if false. */
31 isFilled?: boolean;
32 /** Limits the width of the section */
33 isWidthLimited?: boolean;
34 /** Flag indicating if the section content is center aligned. isWidthLimited must be set for this to work */
35 isCenterAligned?: boolean;
36 /** Padding at various breakpoints. */
37 padding?: {
38 default?: 'padding' | 'noPadding';
39 sm?: 'padding' | 'noPadding';
40 md?: 'padding' | 'noPadding';
41 lg?: 'padding' | 'noPadding';
42 xl?: 'padding' | 'noPadding';
43 '2xl'?: 'padding' | 'noPadding';
44 };
45 /** Modifier indicating if the PageBreadcrumb is sticky to the top or bottom at various breakpoints */
46 stickyOnBreakpoint?: {
47 default?: 'top' | 'bottom';
48 sm?: 'top' | 'bottom';
49 md?: 'top' | 'bottom';
50 lg?: 'top' | 'bottom';
51 xl?: 'top' | 'bottom';
52 '2xl'?: 'top' | 'bottom';
53 };
54 /** @beta Applies the base sticky positioning to the top or bottom of the scroll parent container. */
55 stickyBase?: 'top' | 'bottom';
56 /** @beta Flag indicating if the section has stuck styling, applied when the section is not at the edge of the scroll parent container. */
57 isStickyStuck?: boolean;
58 /** Modifier indicating if PageSection should have a shadow at the top */
59 hasShadowTop?: boolean;
60 /** Modifier indicating if PageSection should have a shadow at the bottom */
61 hasShadowBottom?: boolean;
62 /** Flag indicating if the PageSection has a scrolling overflow */
63 hasOverflowScroll?: boolean;
64 /** @beta Flag indicating whether children passed to the component should be wrapped by a PageBody.
65 * Set this to false in order to pass multiple, custom PageBody's as children.
66 */
67 hasBodyWrapper?: boolean;
68 /** Adds an accessible name to the page section. Required when the hasOverflowScroll prop is set to true.
69 * This prop should also be passed in if a heading is not being used to describe the content of the page section.
70 */
71 'aria-label'?: string;
72 /** Sets the base component to render. Defaults to section */
73 component?: keyof React.JSX.IntrinsicElements;
74 /** Adds plain styling to the page section. */
75 isPlain?: boolean;
76 /** @beta Prevents the page section from automatically applying plain styling when glass theme is enabled. */
77 isNoPlainOnGlass?: boolean;
78 }
79
80 const variantType = {
81 [PageSectionTypes.default]: styles.pageMainSection,
82 [PageSectionTypes.subNav]: styles.pageMainSubnav,
83 [PageSectionTypes.breadcrumb]: styles.pageMainBreadcrumb,
84 [PageSectionTypes.tabs]: styles.pageMainTabs,
85 [PageSectionTypes.wizard]: styles.pageMainWizard
86 };
87
88 const variantStyle = {
89 [PageSectionVariants.default]: '',
90 [PageSectionVariants.secondary]: styles.modifiers.secondary
91 };
92
93 export const PageSection: React.FunctionComponent<PageSectionProps> = ({
94 className = '',
95 children,
96 variant = 'default',
97 type = 'default',
98 padding,
99 isFilled,
100 isWidthLimited = false,
101 isCenterAligned = false,
102 stickyOnBreakpoint,
103 stickyBase,
104 isStickyStuck = false,
105 hasShadowTop = false,
106 hasShadowBottom = false,
107 hasOverflowScroll = false,
108 'aria-label': ariaLabel,
109 component = 'section',
110 hasBodyWrapper = true,
111 isPlain = false,
112 isNoPlainOnGlass = false,
113 ...props
114 }: PageSectionProps) => {
115 const { height, getVerticalBreakpoint } = useContext(PageContext);
116
117 useEffect(() => {
118 if (hasOverflowScroll && !ariaLabel) {
119 /* eslint-disable no-console */
120 console.warn('PageSection: An accessible aria-label is required when hasOverflowScroll is set to true.');
121 }
122 }, [hasOverflowScroll, ariaLabel]);
123
124 const Component = component as any;
125
126 return (
127 <Component
128 {...props}
129 className={css(
130 variantType[type],
131 formatBreakpointMods(padding, styles),
132 formatBreakpointMods(stickyOnBreakpoint, styles, 'sticky-', getVerticalBreakpoint(height), true),
133 stickyBase === 'top' && styles.modifiers.stickyTopBase,
134 stickyBase === 'bottom' && styles.modifiers.stickyBottomBase,
135 isStickyStuck && stickyBase === 'top' && styles.modifiers.stickyTopStuck,
136 isStickyStuck && stickyBase === 'bottom' && styles.modifiers.stickyBottomStuck,
137 type === PageSectionTypes.default && variantStyle[variant],
138 isFilled === false && styles.modifiers.noFill,
139 isFilled === true && styles.modifiers.fill,
140 isWidthLimited && styles.modifiers.limitWidth,
141 isWidthLimited && isCenterAligned && type !== PageSectionTypes.subNav && styles.modifiers.alignCenter,
142 hasShadowTop && styles.modifiers.shadowTop,
143 hasShadowBottom && styles.modifiers.shadowBottom,
144 hasOverflowScroll && styles.modifiers.overflowScroll,
145 isPlain && styles.modifiers.plain,
146 isNoPlainOnGlass && styles.modifiers.noPlainOnGlass,
147 className
148 )}
149 {...(hasOverflowScroll && { tabIndex: 0 })}
150 aria-label={ariaLabel}
151 >
152 {hasBodyWrapper ? <PageBody>{children}</PageBody> : children}
153 </Component>
154 );
155 };
156 PageSection.displayName = 'PageSection';