-
Notifications
You must be signed in to change notification settings - Fork 103
feat(OverflowToolbar): initial implementation #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d489475
feat(OverflowToolbar): initial implementation
Lukas742 046ec82
Merge branch 'master' into feature/overflowToolbar
Lukas742 e9a27f2
Update packages/main/src/components/OverflowToolbar/OverflowToolbar.j…
Lukas742 4a886c5
Update packages/main/src/components/OverflowToolbar/OverflowToolbar.k…
Lukas742 2e23432
Update packages/main/src/components/OverflowToolbar/demo.stories.tsx
Lukas742 a9dc98b
Update packages/main/src/components/OverflowToolbar/index.tsx
Lukas742 13d78bc
Update packages/main/src/components/OverflowToolbar/index.tsx
Lukas742 1b8d6c6
feat(OverflowToolbar): Removed overflow prop from demo
Lukas742 92f80cf
Merge branch 'feature/overflowToolbar' of github.com:SAP/ui5-webcompo…
Lukas742 de050b3
feat(OverflowToolbar): Small enhancements - mainly in readability
Lukas742 dfb1226
Merge branch 'master' into feature/overflowToolbar
MarcusNotheis 1d09fd8
Merge branch 'master' into feature/overflowToolbar
MarcusNotheis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
packages/main/src/components/OverflowToolbar/OverflowToolbar.jss.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { JSSTheme } from '../../interfaces/JSSTheme'; | ||
import { ContentDensity } from '../../lib/ContentDensity'; | ||
|
||
const styles = ({ contentDensity, parameters }: JSSTheme) => ({ | ||
toolbarRoot: { | ||
display: 'flex', | ||
whiteSpace: 'nowrap', | ||
overflow: 'hidden', | ||
alignItems: 'center', | ||
boxSizing: 'border-box', | ||
background: 'transparent', | ||
height: ContentDensity.Compact === contentDensity ? '2rem' : '3rem', | ||
'&:focus': { | ||
outline: 0 | ||
}, | ||
'& :first-child': { | ||
marginLeft: '0.25rem' | ||
}, | ||
'& > *': { | ||
marginRight: '0.5rem !important' // we need the !important here because otherwise e.g. the SearchField has no border | ||
} | ||
}, | ||
toolbarAlignStart: { | ||
justifyContent: 'flex-start' | ||
}, | ||
|
||
toolbarAlignEnd: { | ||
justifyContent: 'flex-end' | ||
}, | ||
|
||
toolbarAlignMiddle: { | ||
justifyContent: 'center' | ||
}, | ||
|
||
toolbarAlignSpaceBetween: { | ||
justifyContent: 'space-between' | ||
}, | ||
pageFooter: { | ||
background: parameters.sapUiPageFooterBackground, | ||
borderTop: `1px solid ${parameters.sapUiPageFooterBorderColor}` | ||
}, | ||
|
||
containerBar: { | ||
background: parameters.sapUiGroupContentBackground | ||
// borderBottom: null | ||
}, | ||
|
||
contentBar: { | ||
background: parameters.sapUiListHeaderBackground, | ||
borderBottom: `1px solid ${parameters.sapUiListHeaderBorderColor}` | ||
}, | ||
|
||
contentBarTransparent: { | ||
background: parameters.sapUiToolbarBackground, | ||
borderBottom: `1px solid ${parameters.sapUiGroupTitleBorderColor}` | ||
} | ||
}); | ||
|
||
export default styles; |
61 changes: 61 additions & 0 deletions
61
packages/main/src/components/OverflowToolbar/OverflowToolbar.karma.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import { OverflowToolbar } from '../../lib/OverflowToolbar'; | ||
import { mountThemedComponent } from '@shared/tests/utils'; | ||
import { expect, use } from 'chai'; | ||
import { ToolbarAlignment } from '../../lib/ToolbarAlignment'; | ||
import { ToolbarDesign } from '../../lib/ToolbarDesign'; | ||
import { Button } from '../../lib/Button'; | ||
import { Label } from '../../lib/Label'; | ||
import { Switch } from '../../lib/Switch'; | ||
import { matchSnapshot } from 'chai-karma-snapshot'; | ||
|
||
use(matchSnapshot); | ||
|
||
const alignDesignFactory = () => { | ||
Object.values(ToolbarDesign).forEach((design) => { | ||
Object.values(ToolbarAlignment).forEach((align) => { | ||
it(`Toolbar with Align:${align} and Design:${design}`, () => { | ||
// console.log(align + design); | ||
expect( | ||
mountThemedComponent( | ||
<OverflowToolbar align={align} toolbarDesign={design}> | ||
Test | ||
</OverflowToolbar> | ||
).debug() | ||
).to.matchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
describe('OverflowToolbar', () => { | ||
alignDesignFactory(); | ||
|
||
it(`Overflow Toolbar with Spacer`, () => { | ||
const wrapper = mountThemedComponent( | ||
<OverflowToolbar overflow> | ||
<span>Test</span> | ||
<span style={{ flexGrow: 1 }} /> | ||
<span>Test 2</span> | ||
</OverflowToolbar> | ||
); | ||
expect(wrapper.render().children().length).equal(3); | ||
expect(wrapper.debug()).to.matchSnapshot(); | ||
}); | ||
|
||
it(`Overflow Toolbar collapsed Elements`, () => { | ||
const wrapper = mountThemedComponent( | ||
<OverflowToolbar overflow width="50px"> | ||
<Label style={{ minWidth: '20px' }}>Label</Label> | ||
<Button>Button1</Button> | ||
<Button>Btn2</Button> | ||
<Button>3</Button> | ||
<Button>4</Button> | ||
<Button>5</Button> | ||
<Switch /> | ||
</OverflowToolbar> | ||
); | ||
expect(wrapper.update().children().length).equal(1); | ||
expect(wrapper.debug()).to.matchSnapshot(); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/main/src/components/OverflowToolbar/demo.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { select } from '@storybook/addon-knobs'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { OverflowToolbar } from '../../lib/OverflowToolbar'; | ||
import { ToolbarAlignment } from '../../lib/ToolbarAlignment'; | ||
import { ToolbarDesign } from '../../lib/ToolbarDesign'; | ||
import { Button } from '../../lib/Button'; | ||
import { Label } from '../../lib/Label'; | ||
import { Switch } from '../../lib/Switch'; | ||
|
||
const renderOverflowToolbar = () => ( | ||
<OverflowToolbar | ||
align={select('align', ToolbarAlignment, ToolbarAlignment.Start)} | ||
toolbarDesign={select('design', ToolbarDesign, ToolbarDesign.ContentBar)} | ||
> | ||
<Label style={{ minWidth: '20px' }}>Label</Label> | ||
<Button>Button1</Button> | ||
<Button>Btn2</Button> | ||
<Button>3</Button> | ||
<Button>4</Button> | ||
<Button>5</Button> | ||
<Switch /> | ||
</OverflowToolbar> | ||
); | ||
|
||
storiesOf('Components | Overflow Toolbar', module).add('Overflow Toolbar', renderOverflowToolbar); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
import React, { Children, Component, CSSProperties, ReactNode, ReactNodeArray, RefObject } from 'react'; | ||
import styles from './OverflowToolbar.jss'; | ||
import { StyleClassHelper, withStyles } from '@ui5/webcomponents-react-base'; | ||
import { ToolbarAlignment } from '../../lib/ToolbarAlignment'; | ||
import { ToolbarDesign } from '../../lib/ToolbarDesign'; | ||
import { CommonProps } from '../../interfaces/CommonProps'; | ||
import { ClassProps } from '../../interfaces/ClassProps'; | ||
import { Popover } from '../../lib/Popover'; | ||
import { Button } from '../../lib/Button'; | ||
import { PlacementType } from '../../lib/PlacementType'; | ||
import boot from '@ui5/webcomponents-base/src/boot'; | ||
|
||
export interface ToolbarPropTypes extends CommonProps { | ||
width?: CSSProperties['width']; | ||
|
||
children: ReactNode | ReactNodeArray; | ||
|
||
align?: ToolbarAlignment; | ||
|
||
toolbarDesign?: ToolbarDesign; | ||
|
||
overflow?: boolean; | ||
} | ||
|
||
interface ToolbarInternalProps extends ToolbarPropTypes, ClassProps {} | ||
|
||
@withStyles(styles) | ||
export class OverflowToolbar extends Component<ToolbarInternalProps> { | ||
static defaultProps = { | ||
width: 'auto', | ||
align: ToolbarAlignment.Start, | ||
toolbarDesign: ToolbarDesign.ContentBar, | ||
overflow: true | ||
}; | ||
state = { | ||
children: this.props.children, | ||
toolbarWidth: 0, | ||
popoverElements: [], | ||
popoverOpen: true, | ||
renderToggle: false, | ||
previousWidth: [] | ||
}; | ||
private toolbarRef: RefObject<HTMLDivElement> = React.createRef(); | ||
|
||
componentDidMount(): void { | ||
boot().then(() => { | ||
if (this.props.overflow) { | ||
if (this.state.toolbarWidth === 0 && this.toolbarRef.current !== null) { | ||
this.setState({ toolbarWidth: this.toolbarRef.current.scrollWidth }); | ||
} | ||
window.addEventListener('resize', () => { | ||
requestAnimationFrame(() => { | ||
this.handleResize(); | ||
}); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
componentDidUpdate(prevProps: Readonly<ToolbarInternalProps>, prevState, snapshot?: any): void { | ||
boot().then(() => { | ||
if (this.props.overflow && this.toolbarRef.current !== null) { | ||
if (this.toolbarRef.current.clientWidth < this.toolbarRef.current.scrollWidth) { | ||
this.handleResize(); | ||
} | ||
if (this.props.width !== prevProps.width) { | ||
this.handleResize(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
componentWillUnmount(): void { | ||
window.removeEventListener('resize', this.handleResize); | ||
} | ||
|
||
private handleResize = () => { | ||
const toolbarRef = this.toolbarRef.current; | ||
let newChildren = this.state.children; | ||
let { popoverElements, renderToggle, previousWidth, toolbarWidth, children } = this.state; | ||
if (toolbarWidth > toolbarRef.clientWidth) { | ||
if (toolbarRef.clientWidth < toolbarRef.scrollWidth) { | ||
newChildren = Children.toArray(children).slice(0, -1); | ||
popoverElements = [Children.toArray(children).slice(-1)[0]].concat(popoverElements); | ||
renderToggle = true; | ||
previousWidth = [toolbarRef.scrollWidth + 10].concat(previousWidth); | ||
} | ||
this.setState({ | ||
toolbarWidth: toolbarRef.scrollWidth, | ||
children: newChildren, | ||
renderToggle, | ||
popoverElements, | ||
previousWidth | ||
}); | ||
} | ||
if (toolbarWidth < toolbarRef.clientWidth) { | ||
this.addToolbarItem(newChildren, popoverElements, previousWidth, renderToggle); | ||
} | ||
}; | ||
|
||
private addToolbarItem(newChildren, popoverElements, previousWidth, renderToggle) { | ||
const toolbarRef = this.toolbarRef.current; | ||
let { children } = this.state; | ||
if (Children.count(this.props.children) !== Children.count(children)) { | ||
if (toolbarRef.clientWidth === toolbarRef.scrollWidth && toolbarRef.clientWidth >= previousWidth[0]) { | ||
const currentChildrenLength = Children.count(children); | ||
newChildren = Children.toArray(children).concat([ | ||
Children.only(Children.toArray(this.props.children)[currentChildrenLength]) | ||
]); | ||
popoverElements.shift(); | ||
previousWidth.shift(); | ||
} | ||
} | ||
this.setState({ | ||
toolbarWidth: toolbarRef.clientWidth, | ||
children: newChildren, | ||
popoverElements, | ||
previousWidth | ||
}); | ||
if (Children.count(this.props.children) !== Children.count(children)) { | ||
if (toolbarRef.clientWidth === toolbarRef.scrollWidth && toolbarRef.clientWidth >= previousWidth[0]) { | ||
this.addToolbarItem(newChildren, popoverElements, previousWidth, renderToggle); | ||
} | ||
} else { | ||
this.setState({ renderToggle: false }); | ||
} | ||
} | ||
|
||
render() { | ||
const { width, align, toolbarDesign, classes, className, style, tooltip } = this.props; | ||
const rootClasses = StyleClassHelper.of(classes.toolbarRoot); | ||
const overflowClasses = StyleClassHelper.of(classes.overflowRoot); | ||
|
||
switch (align) { | ||
case ToolbarAlignment.Start: | ||
rootClasses.put(classes.toolbarAlignStart); | ||
break; | ||
case ToolbarAlignment.End: | ||
rootClasses.put(classes.toolbarAlignEnd); | ||
break; | ||
case ToolbarAlignment.Middle: | ||
rootClasses.put(classes.toolbarAlignMiddle); | ||
break; | ||
case ToolbarAlignment.SpaceBetween: | ||
rootClasses.put(classes.toolbarAlignSpaceBetween); | ||
break; | ||
default: | ||
rootClasses.put(classes.toolbarAlignStart); | ||
} | ||
|
||
switch (toolbarDesign) { | ||
case ToolbarDesign.ContentBar: | ||
rootClasses.put(classes.contentBar); | ||
overflowClasses.put(classes.contentBar); | ||
break; | ||
case ToolbarDesign.PageFooter: | ||
rootClasses.put(classes.pageFooter); | ||
overflowClasses.put(classes.pageFooter); | ||
break; | ||
case ToolbarDesign.ContainerBar: | ||
rootClasses.put(classes.containerBar); | ||
overflowClasses.put(classes.containerBar); | ||
break; | ||
case ToolbarDesign.ContentBarTransparent: | ||
rootClasses.put(classes.contentBarTransparent); | ||
overflowClasses.put(classes.contentBarTransparent); | ||
break; | ||
default: | ||
rootClasses.put(classes.containerBar); | ||
overflowClasses.put(classes.containerBar); | ||
} | ||
|
||
if (className) { | ||
rootClasses.put(className); | ||
} | ||
|
||
const inlineStyle = { width }; | ||
if (style) { | ||
Object.assign(inlineStyle, style); | ||
} | ||
return ( | ||
<div className={rootClasses.valueOf()} style={{ ...inlineStyle }} title={tooltip} ref={this.toolbarRef}> | ||
{this.state.children} | ||
{this.state.renderToggle && ( | ||
<Popover | ||
style={{ marginLeft: 'auto' }} | ||
key="popover" | ||
openBy={<Button icon="overflow" key={'overflowButton'} />} | ||
noHeader={true} | ||
placementType={PlacementType.Bottom} | ||
> | ||
<div style={{ display: 'flex', flexDirection: 'column' }}>{this.state.popoverElements}</div> | ||
</Popover> | ||
)} | ||
</div> | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export enum ToolbarAlignment { | ||
Start = 'Start', | ||
Middle = 'Middle', | ||
End = 'End', | ||
SpaceBetween = 'SpaceBetween' | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export enum ToolbarDesign { | ||
PageFooter = 'PageFooter', | ||
ContainerBar = 'ContainerBar', | ||
ContentBar = 'ContentBar', | ||
ContentBarTransparent = 'ContentBarTransparent' | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { OverflowToolbar } from '../components/OverflowToolbar'; | ||
|
||
export { OverflowToolbar }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.