-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Coalesce UI filtering menus (#4972)
Signed-off-by: Simon Behar <simbeh7@gmail.com>
- Loading branch information
Showing
7 changed files
with
284 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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,67 @@ | ||
@import 'node_modules/argo-ui/src/styles/config'; | ||
|
||
.argo-dropdown { | ||
display: inline-block; | ||
|
||
&__anchor { | ||
cursor: pointer; | ||
} | ||
|
||
&__content { | ||
position: fixed; | ||
z-index: $notifications-z-index; | ||
padding: 1rem; | ||
background-color: $white-color; | ||
box-shadow: 0 0 4px rgba(#000, .2); | ||
transition: opacity .2s, transform .2s, visibility .2s; | ||
|
||
&:not(.opened) { | ||
transform: translateY(-30%); | ||
opacity: 0; | ||
visibility: hidden; | ||
transition: opacity .2s, transform .2s .2s, visibility .2s; | ||
} | ||
|
||
&.is-menu { | ||
overflow: auto; | ||
max-height: 600px; | ||
padding: 10px; | ||
border: 0; | ||
|
||
ul { | ||
margin: 0; | ||
list-style-type: none; | ||
white-space: nowrap; | ||
text-align: left; | ||
cursor: pointer; | ||
min-width: 150px; | ||
column-count: 3; | ||
column-gap: 10px; | ||
|
||
li { | ||
padding: 0.5em 1em; | ||
font-size: 14px; | ||
color: $argo-color-gray-6; | ||
cursor: pointer; | ||
border-bottom: none !important; | ||
|
||
i { | ||
margin-right: 2px; | ||
} | ||
|
||
&.title { | ||
font-weight: bolder; | ||
} | ||
|
||
&:hover { | ||
background-color: $argo-color-gray-1; | ||
} | ||
|
||
&:last-child { | ||
border-bottom: none; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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,127 @@ | ||
import * as classNames from 'classnames'; | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import {BehaviorSubject, Observable, Subscription} from 'rxjs'; | ||
|
||
export interface DropDownProps { | ||
isMenu?: boolean; | ||
anchor: React.ComponentType; | ||
children: React.ReactNode | (() => React.ReactNode); | ||
qeId?: string; | ||
} | ||
|
||
export interface DropDownState { | ||
opened: boolean; | ||
left: number; | ||
top: number; | ||
} | ||
|
||
require('./dropdown.scss'); | ||
|
||
const dropDownOpened = new BehaviorSubject<DropDown>(null); | ||
|
||
export class DropDown extends React.Component<DropDownProps, DropDownState> { | ||
private el: HTMLDivElement; | ||
private content: HTMLDivElement; | ||
private subscriptions: Subscription[]; | ||
|
||
constructor(props: DropDownProps) { | ||
super(props); | ||
this.state = {opened: false, left: 0, top: 0}; | ||
} | ||
|
||
public render() { | ||
let children: React.ReactNode = null; | ||
if (typeof this.props.children === 'function') { | ||
if (this.state.opened) { | ||
const fun = this.props.children as () => React.ReactNode; | ||
children = fun(); | ||
} | ||
} else { | ||
children = this.props.children as React.ReactNode; | ||
} | ||
|
||
return ( | ||
<div className='argo-dropdown' ref={el => (this.el = el)}> | ||
<div | ||
qe-id={this.props.qeId} | ||
className='argo-dropdown__anchor' | ||
onClick={event => { | ||
this.open(); | ||
event.stopPropagation(); | ||
}}> | ||
<this.props.anchor /> | ||
</div> | ||
{ReactDOM.createPortal( | ||
<div | ||
className={classNames('argo-dropdown__content', {'opened': this.state.opened, 'is-menu': this.props.isMenu})} | ||
style={{top: this.state.top, left: this.state.left}} | ||
ref={el => (this.content = el)}> | ||
{children} | ||
</div>, | ||
document.body | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
public componentWillMount() { | ||
this.subscriptions = [ | ||
Observable.merge( | ||
dropDownOpened.filter(dropdown => dropdown !== this), | ||
Observable.fromEvent(document, 'click').filter((event: Event) => { | ||
return this.content && this.state.opened && !this.content.contains(event.target as Node) && !this.el.contains(event.target as Node); | ||
}) | ||
).subscribe(() => { | ||
this.close(); | ||
}), | ||
Observable.fromEvent(document, 'scroll', true).subscribe(() => { | ||
if (this.state.opened && this.content && this.el) { | ||
this.setState(this.refreshState()); | ||
} | ||
}) | ||
]; | ||
} | ||
|
||
public componentWillUnmount() { | ||
(this.subscriptions || []).forEach(s => s.unsubscribe()); | ||
this.subscriptions = []; | ||
} | ||
|
||
public close() { | ||
this.setState({opened: false}); | ||
} | ||
|
||
private refreshState() { | ||
const anchor = this.el.querySelector('.argo-dropdown__anchor') as HTMLElement; | ||
const {top, left} = anchor.getBoundingClientRect(); | ||
const anchorHeight = anchor.offsetHeight + 2; | ||
|
||
const newState = {left: this.state.left, top: this.state.top, opened: this.state.opened}; | ||
// Set top position | ||
if (this.content.offsetHeight + top + anchorHeight > window.innerHeight) { | ||
newState.top = top - this.content.offsetHeight - 2; | ||
} else { | ||
newState.top = top + anchorHeight; | ||
} | ||
|
||
// Set left position | ||
if (this.content.offsetWidth + left > window.innerWidth) { | ||
newState.left = left - this.content.offsetWidth + anchor.offsetWidth; | ||
} else { | ||
newState.left = left; | ||
} | ||
return newState; | ||
} | ||
|
||
private open() { | ||
if (!this.content || !this.el) { | ||
return; | ||
} | ||
|
||
const newState = this.refreshState(); | ||
newState.opened = true; | ||
this.setState(newState); | ||
dropDownOpened.next(this); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,16 +1,49 @@ | ||
import {DropDown} from 'argo-ui/src/components/dropdown/dropdown'; | ||
import {Checkbox} from 'argo-ui'; | ||
import * as classNames from 'classnames'; | ||
import * as React from 'react'; | ||
import {CheckboxList} from './checkbox-list'; | ||
import {DropDown} from './dropdown/dropdown'; | ||
|
||
export const FilterDropDown = (props: {values: {[label: string]: boolean}; onChange: (label: string, checked: boolean) => void}) => ( | ||
<DropDown | ||
isMenu={true} | ||
anchor={() => ( | ||
<div className={'top-bar__filter' + (props.values.size > props.values.size ? ' top-bar__filter--selected' : '')}> | ||
<i className='argo-icon-filter' /> | ||
<i className='fa fa-angle-down' /> | ||
</div> | ||
)}> | ||
<CheckboxList values={props.values} onChange={(label, checked) => props.onChange(label, checked)} /> | ||
</DropDown> | ||
); | ||
interface FilterDropDownProps { | ||
sections: FilterDropSection[]; | ||
} | ||
|
||
export interface FilterDropSection { | ||
title: string; | ||
values: {[label: string]: boolean}; | ||
onChange: (label: string, checked: boolean) => void; | ||
} | ||
|
||
export const FilterDropDown = (props: FilterDropDownProps) => { | ||
return ( | ||
<DropDown | ||
isMenu={true} | ||
anchor={() => ( | ||
<div className={classNames('top-bar__filter', {'top-bar__filter--selected': true})} title='Filter'> | ||
<i className='argo-icon-filter' aria-hidden='true' /> | ||
<i className='fa fa-angle-down' aria-hidden='true' /> | ||
</div> | ||
)}> | ||
<ul> | ||
{props.sections | ||
.filter(item => item.values) | ||
.map((item, i) => ( | ||
<div key={i}> | ||
<li className={classNames('top-bar__filter-item', {title: true})}> | ||
<span>{item.title}</span> | ||
</li> | ||
{Object.entries(item.values) | ||
.sort() | ||
.map(([label, checked]) => ( | ||
<li className={classNames('top-bar__filter-item')}> | ||
<React.Fragment> | ||
<Checkbox id={`filter__${label}`} checked={checked} onChange={v => item.onChange(label, v)} /> | ||
<label htmlFor={`filter__${label}`}>{label}</label> | ||
</React.Fragment> | ||
</li> | ||
))} | ||
</div> | ||
))} | ||
</ul> | ||
</DropDown> | ||
); | ||
}; |
This file contains 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 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 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