| 1 |
import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import { formatBreakpointMods, toCamel } from '../../helpers/util'; |
| 4 |
import { Divider } from '../Divider'; |
| 5 |
import { PageContext } from '../Page/PageContext'; |
| 6 |
|
| 7 |
export enum ToolbarItemVariant { |
| 8 |
separator = 'separator', |
| 9 |
pagination = 'pagination', |
| 10 |
label = 'label', |
| 11 |
'label-group' = 'label-group', |
| 12 |
'expand-all' = 'expand-all' |
| 13 |
} |
| 14 |
|
| 15 |
export interface ToolbarItemProps extends React.HTMLProps<HTMLDivElement> { |
| 16 |
/** Classes applied to root element of the data toolbar item */ |
| 17 |
className?: string; |
| 18 |
/** A type modifier which modifies spacing specifically depending on the type of item */ |
| 19 |
variant?: ToolbarItemVariant | 'pagination' | 'label' | 'label-group' | 'separator' | 'expand-all'; |
| 20 |
/** Visibility at various width breakpoints. */ |
| 21 |
visibility?: { |
| 22 |
default?: 'hidden' | 'visible'; |
| 23 |
md?: 'hidden' | 'visible'; |
| 24 |
lg?: 'hidden' | 'visible'; |
| 25 |
xl?: 'hidden' | 'visible'; |
| 26 |
'2xl'?: 'hidden' | 'visible'; |
| 27 |
}; |
| 28 |
/** Visibility at various height breakpoints. */ |
| 29 |
visibilityAtHeight?: { |
| 30 |
default?: 'hidden' | 'visible'; |
| 31 |
md?: 'hidden' | 'visible'; |
| 32 |
lg?: 'hidden' | 'visible'; |
| 33 |
xl?: 'hidden' | 'visible'; |
| 34 |
'2xl'?: 'hidden' | 'visible'; |
| 35 |
}; |
| 36 |
/** Applies to a child of a flex layout, and aligns that child (and any adjacent children on the other side of it) to one side of the main axis */ |
| 37 |
align?: { |
| 38 |
default?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 39 |
md?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 40 |
lg?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 41 |
xl?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 42 |
'2xl'?: 'alignEnd' | 'alignStart' | 'alignCenter'; |
| 43 |
}; |
| 44 |
/** Vertical alignment of children */ |
| 45 |
alignItems?: 'start' | 'center' | 'baseline' | 'default' | 'end' | 'stretch'; |
| 46 |
/** Vertical alignment */ |
| 47 |
alignSelf?: 'start' | 'center' | 'baseline' | 'default' | 'end' | 'stretch'; |
| 48 |
/** Sets both the column and row gap at various breakpoints. */ |
| 49 |
gap?: { |
| 50 |
default?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 51 |
md?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 52 |
lg?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 53 |
xl?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 54 |
'2xl'?: 'gapNone' | 'gapXs' | 'gapSm' | 'gapMd' | 'gapLg' | 'gapXl' | 'gap_2xl' | 'gap_3xl' | 'gap_4xl'; |
| 55 |
}; |
| 56 |
/** Sets only the column gap at various breakpoints. */ |
| 57 |
columnGap?: { |
| 58 |
default?: |
| 59 |
| 'columnGapNone' |
| 60 |
| 'columnGapXs' |
| 61 |
| 'columnGapSm' |
| 62 |
| 'columnGapMd' |
| 63 |
| 'columnGapLg' |
| 64 |
| 'columnGapXl' |
| 65 |
| 'columnGap_2xl' |
| 66 |
| 'columnGap_3xl' |
| 67 |
| 'columnGap_4xl'; |
| 68 |
md?: |
| 69 |
| 'columnGapNone' |
| 70 |
| 'columnGapXs' |
| 71 |
| 'columnGapSm' |
| 72 |
| 'columnGapMd' |
| 73 |
| 'columnGapLg' |
| 74 |
| 'columnGapXl' |
| 75 |
| 'columnGap_2xl' |
| 76 |
| 'columnGap_3xl' |
| 77 |
| 'columnGap_4xl'; |
| 78 |
lg?: |
| 79 |
| 'columnGapNone' |
| 80 |
| 'columnGapXs' |
| 81 |
| 'columnGapSm' |
| 82 |
| 'columnGapMd' |
| 83 |
| 'columnGapLg' |
| 84 |
| 'columnGapXl' |
| 85 |
| 'columnGap_2xl' |
| 86 |
| 'columnGap_3xl' |
| 87 |
| 'columnGap_4xl'; |
| 88 |
xl?: |
| 89 |
| 'columnGapNone' |
| 90 |
| 'columnGapXs' |
| 91 |
| 'columnGapSm' |
| 92 |
| 'columnGapMd' |
| 93 |
| 'columnGapLg' |
| 94 |
| 'columnGapXl' |
| 95 |
| 'columnGap_2xl' |
| 96 |
| 'columnGap_3xl' |
| 97 |
| 'columnGap_4xl'; |
| 98 |
'2xl'?: |
| 99 |
| 'columnGapNone' |
| 100 |
| 'columnGapXs' |
| 101 |
| 'columnGapSm' |
| 102 |
| 'columnGapMd' |
| 103 |
| 'columnGapLg' |
| 104 |
| 'columnGapXl' |
| 105 |
| 'columnGap_2xl' |
| 106 |
| 'columnGap_3xl' |
| 107 |
| 'columnGap_4xl'; |
| 108 |
}; |
| 109 |
/** Sets only the row gap at various breakpoints. */ |
| 110 |
rowGap?: { |
| 111 |
default?: |
| 112 |
| 'rowGapNone' |
| 113 |
| 'rowGapXs' |
| 114 |
| 'rowGapSm' |
| 115 |
| 'rowGapMd' |
| 116 |
| 'rowGapLg' |
| 117 |
| 'rowGapXl' |
| 118 |
| 'rowGap_2xl' |
| 119 |
| 'rowGap_3xl' |
| 120 |
| 'rowGap_4xl'; |
| 121 |
md?: |
| 122 |
| 'rowGapNone' |
| 123 |
| 'rowGapXs' |
| 124 |
| 'rowGapSm' |
| 125 |
| 'rowGapMd' |
| 126 |
| 'rowGapLg' |
| 127 |
| 'rowGapXl' |
| 128 |
| 'rowGap_2xl' |
| 129 |
| 'rowGap_3xl' |
| 130 |
| 'rowGap_4xl'; |
| 131 |
lg?: |
| 132 |
| 'rowGapNone' |
| 133 |
| 'rowGapXs' |
| 134 |
| 'rowGapSm' |
| 135 |
| 'rowGapMd' |
| 136 |
| 'rowGapLg' |
| 137 |
| 'rowGapXl' |
| 138 |
| 'rowGap_2xl' |
| 139 |
| 'rowGap_3xl' |
| 140 |
| 'rowGap_4xl'; |
| 141 |
xl?: |
| 142 |
| 'rowGapNone' |
| 143 |
| 'rowGapXs' |
| 144 |
| 'rowGapSm' |
| 145 |
| 'rowGapMd' |
| 146 |
| 'rowGapLg' |
| 147 |
| 'rowGapXl' |
| 148 |
| 'rowGap_2xl' |
| 149 |
| 'rowGap_3xl' |
| 150 |
| 'rowGap_4xl'; |
| 151 |
'2xl'?: |
| 152 |
| 'rowGapNone' |
| 153 |
| 'rowGapXs' |
| 154 |
| 'rowGapSm' |
| 155 |
| 'rowGapMd' |
| 156 |
| 'rowGapLg' |
| 157 |
| 'rowGapXl' |
| 158 |
| 'rowGap_2xl' |
| 159 |
| 'rowGap_3xl' |
| 160 |
| 'rowGap_4xl'; |
| 161 |
}; |
| 162 |
/** Value to set for row wrapping at various breakpoints */ |
| 163 |
rowWrap?: { |
| 164 |
default?: 'wrap' | 'nowrap'; |
| 165 |
sm?: 'wrap' | 'nowrap'; |
| 166 |
md?: 'wrap' | 'nowrap'; |
| 167 |
lg?: 'wrap' | 'nowrap'; |
| 168 |
xl?: 'wrap' | 'nowrap'; |
| 169 |
'2xl'?: 'wrap' | 'nowrap'; |
| 170 |
}; |
| 171 |
/** id for this data toolbar item */ |
| 172 |
id?: string; |
| 173 |
/** Flag indicating if the expand-all variant is expanded or not */ |
| 174 |
isAllExpanded?: boolean; |
| 175 |
/** Flag that modifies the toolbar item to hide overflow and respond to available space. Used for horizontal navigation. */ |
| 176 |
isOverflowContainer?: boolean; |
| 177 |
/** Content to be rendered inside the data toolbar item */ |
| 178 |
children?: React.ReactNode; |
| 179 |
} |
| 180 |
|
| 181 |
export const ToolbarItem: React.FunctionComponent<ToolbarItemProps> = ({ |
| 182 |
className, |
| 183 |
variant, |
| 184 |
visibility, |
| 185 |
visibilityAtHeight, |
| 186 |
gap, |
| 187 |
columnGap, |
| 188 |
rowGap, |
| 189 |
rowWrap, |
| 190 |
align, |
| 191 |
alignSelf, |
| 192 |
alignItems, |
| 193 |
id, |
| 194 |
children, |
| 195 |
isAllExpanded, |
| 196 |
isOverflowContainer, |
| 197 |
role, |
| 198 |
...props |
| 199 |
}: ToolbarItemProps) => { |
| 200 |
if (variant === ToolbarItemVariant.separator) { |
| 201 |
const isDividerRoleValid = role === 'separator' || role === 'presentation'; |
| 202 |
return ( |
| 203 |
<Divider |
| 204 |
className={css(className)} |
| 205 |
orientation={{ default: 'vertical' }} |
| 206 |
{...props} |
| 207 |
{...(isDividerRoleValid && { role: role as 'separator' | 'presentation' })} |
| 208 |
/> |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
return ( |
| 213 |
<PageContext.Consumer> |
| 214 |
{({ width, getBreakpoint, height, getVerticalBreakpoint }) => ( |
| 215 |
<div |
| 216 |
className={css( |
| 217 |
styles.toolbarItem, |
| 218 |
variant && styles.modifiers[toCamel(variant) as 'pagination' | 'label'], |
| 219 |
variant === ToolbarItemVariant['label-group'] && styles.modifiers.labelGroup, |
| 220 |
isAllExpanded && styles.modifiers.expanded, |
| 221 |
isOverflowContainer && styles.modifiers.overflowContainer, |
| 222 |
formatBreakpointMods(visibility, styles, '', getBreakpoint(width)), |
| 223 |
formatBreakpointMods(visibilityAtHeight, styles, '', getVerticalBreakpoint(height), true), |
| 224 |
formatBreakpointMods(align, styles, '', getBreakpoint(width)), |
| 225 |
formatBreakpointMods(gap, styles, '', getBreakpoint(width)), |
| 226 |
formatBreakpointMods(columnGap, styles, '', getBreakpoint(width)), |
| 227 |
formatBreakpointMods(rowGap, styles, '', getBreakpoint(width)), |
| 228 |
formatBreakpointMods(rowWrap, styles, '', getBreakpoint(width)), |
| 229 |
alignItems === 'start' && styles.modifiers.alignItemsStart, |
| 230 |
alignItems === 'center' && styles.modifiers.alignItemsCenter, |
| 231 |
alignItems === 'baseline' && styles.modifiers.alignItemsBaseline, |
| 232 |
alignSelf === 'start' && styles.modifiers.alignSelfStart, |
| 233 |
alignSelf === 'center' && styles.modifiers.alignSelfCenter, |
| 234 |
alignSelf === 'baseline' && styles.modifiers.alignSelfBaseline, |
| 235 |
className |
| 236 |
)} |
| 237 |
{...(variant === 'label' && { 'aria-hidden': true })} |
| 238 |
id={id} |
| 239 |
role={role} |
| 240 |
{...props} |
| 241 |
> |
| 242 |
{children} |
| 243 |
</div> |
| 244 |
)} |
| 245 |
</PageContext.Consumer> |
| 246 |
); |
| 247 |
}; |
| 248 |
ToolbarItem.displayName = 'ToolbarItem'; |