forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WiP] Switch content-type toggle to dropdown, or at least try
- Loading branch information
1 parent
d2f4e1e
commit 6bcbf77
Showing
2 changed files
with
211 additions
and
23 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
125 changes: 125 additions & 0 deletions
125
app/javascript/flavours/glitch/features/compose/components/dropdown_menu.jsx
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,125 @@ | ||
import PropTypes from 'prop-types'; | ||
import { PureComponent } from 'react'; | ||
|
||
import classNames from 'classnames'; | ||
|
||
import { supportsPassiveEvents } from 'detect-passive-events'; | ||
|
||
import { Icon } from 'flavours/glitch/components/icon'; | ||
|
||
const listenerOptions = supportsPassiveEvents ? { passive: true, capture: true } : true; | ||
|
||
// copied from PrivacyDropdown; will require refactor with upstream down the line | ||
class DropdownMenu extends PureComponent { | ||
|
||
static propTypes = { | ||
style: PropTypes.object, | ||
items: PropTypes.array.isRequired, | ||
value: PropTypes.string.isRequired, | ||
onClose: PropTypes.func.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
}; | ||
|
||
handleDocumentClick = e => { | ||
if (this.node && !this.node.contains(e.target)) { | ||
this.props.onClose(); | ||
e.stopPropagation(); | ||
} | ||
}; | ||
|
||
handleKeyDown = e => { | ||
const { items } = this.props; | ||
const value = e.currentTarget.getAttribute('data-index'); | ||
const index = items.findIndex(item => { | ||
return (item.value === value); | ||
}); | ||
let element = null; | ||
|
||
switch(e.key) { | ||
case 'Escape': | ||
this.props.onClose(); | ||
break; | ||
case 'Enter': | ||
this.handleClick(e); | ||
break; | ||
case 'ArrowDown': | ||
element = this.node.childNodes[index + 1] || this.node.firstChild; | ||
break; | ||
case 'ArrowUp': | ||
element = this.node.childNodes[index - 1] || this.node.lastChild; | ||
break; | ||
case 'Tab': | ||
if (e.shiftKey) { | ||
element = this.node.childNodes[index - 1] || this.node.lastChild; | ||
} else { | ||
element = this.node.childNodes[index + 1] || this.node.firstChild; | ||
} | ||
break; | ||
case 'Home': | ||
element = this.node.firstChild; | ||
break; | ||
case 'End': | ||
element = this.node.lastChild; | ||
break; | ||
} | ||
|
||
if (element) { | ||
element.focus(); | ||
this.props.onChange(element.getAttribute('data-index')); | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
} | ||
}; | ||
|
||
handleClick = e => { | ||
const value = e.currentTarget.getAttribute('data-index'); | ||
|
||
e.preventDefault(); | ||
|
||
this.props.onClose(); | ||
this.props.onChange(value); | ||
}; | ||
|
||
componentDidMount () { | ||
document.addEventListener('click', this.handleDocumentClick, { capture: true }); | ||
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions); | ||
if (this.focusedItem) this.focusedItem.focus({ preventScroll: true }); | ||
} | ||
|
||
componentWillUnmount () { | ||
document.removeEventListener('click', this.handleDocumentClick, { capture: true }); | ||
document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions); | ||
} | ||
|
||
setRef = c => { | ||
this.node = c; | ||
}; | ||
|
||
setFocusRef = c => { | ||
this.focusedItem = c; | ||
}; | ||
|
||
render () { | ||
const { style, items, value } = this.props; | ||
|
||
return ( | ||
<div style={{ ...style }} role='listbox' ref={this.setRef}> | ||
{items.map(item => ( | ||
<div role='option' tabIndex={0} key={item.value} data-index={item.value} onKeyDown={this.handleKeyDown} onClick={this.handleClick} className={classNames('privacy-dropdown__option', { active: item.value === value })} aria-selected={item.value === value} ref={item.value === value ? this.setFocusRef : null}> | ||
<div className='privacy-dropdown__option__icon'> | ||
<Icon id={item.icon} icon={item.iconComponent} /> | ||
</div> | ||
|
||
<div className='privacy-dropdown__option__content'> | ||
<strong>{item.text}</strong> | ||
{item.meta} | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
} | ||
|
||
} | ||
|
||
export default DropdownMenu; |