app
assets
controllers
helpers
javascript
fonts
images
mastodon
actions
components
account.js
attachment_list.js
autosuggest_textarea.js
avatar.js
avatar_overlay.js
button.js
collapsable.js
column_back_button.js
column_back_button_slim.js
column_collapsable.js
display_name.js
dropdown_menu.js
extended_video_player.js
icon_button.js
load_more.js
loading_indicator.js
media_gallery.js
missing_indicator.js
permalink.js
relative_timestamp.js
status.js
status_action_bar.js
status_content.js
status_list.js
video_player.js
containers
features
locales
middleware
reducers
selectors
store
.gitkeep
api.js
base_polyfills.js
emoji.js
extra_polyfills.js
is_mobile.js
link_header.js
main.js
rtl.js
stream.js
uuid.js
packs
styles
lib
mailers
models
presenters
services
validators
views
workers
bin
config
db
docs
lib
log
nanobox
public
spec
storybook
streaming
vendor
.babelrc
.buildpacks
.codeclimate.yml
.dockerignore
.editorconfig
.env.nanobox
.env.production.sample
.env.test
.env.vagrant
.eslintignore
.eslintrc.yml
.foreman
.gitignore
.haml-lint.yml
.nanoignore
.nvmrc
.postcssrc.yml
.profile
.rspec
.rubocop.yml
.ruby-version
.scss-lint.yml
.slugignore
.travis.yml
Aptfile
CONTRIBUTING.md
Capfile
Dockerfile
Gemfile
Gemfile.lock
ISSUE_TEMPLATE.md
LICENSE
Procfile
Procfile.dev
README.md
Rakefile
Vagrantfile
app.json
boxfile.yml
config.ru
docker-compose.yml
docker_entrypoint.sh
package.json
scalingo.json
yarn.lock
97 lines
2.5 KiB
JavaScript
97 lines
2.5 KiB
JavaScript
import React from 'react';
|
|
import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
|
|
import PropTypes from 'prop-types';
|
|
|
|
class DropdownMenu extends React.PureComponent {
|
|
|
|
static contextTypes = {
|
|
router: PropTypes.object,
|
|
};
|
|
|
|
static propTypes = {
|
|
icon: PropTypes.string.isRequired,
|
|
items: PropTypes.array.isRequired,
|
|
size: PropTypes.number.isRequired,
|
|
direction: PropTypes.string,
|
|
ariaLabel: PropTypes.string,
|
|
};
|
|
|
|
static defaultProps = {
|
|
ariaLabel: "Menu",
|
|
};
|
|
|
|
state = {
|
|
direction: 'left',
|
|
expanded: false,
|
|
};
|
|
|
|
setRef = (c) => {
|
|
this.dropdown = c;
|
|
}
|
|
|
|
handleClick = (e) => {
|
|
const i = Number(e.currentTarget.getAttribute('data-index'));
|
|
const { action, to } = this.props.items[i];
|
|
|
|
// Don't call e.preventDefault() when the item uses 'href' property.
|
|
// ex. "Edit profile" on the account action bar
|
|
|
|
if (typeof action === 'function') {
|
|
e.preventDefault();
|
|
action();
|
|
} else if (to) {
|
|
e.preventDefault();
|
|
this.context.router.push(to);
|
|
}
|
|
|
|
this.dropdown.hide();
|
|
}
|
|
|
|
handleShow = () => this.setState({ expanded: true })
|
|
|
|
handleHide = () => this.setState({ expanded: false })
|
|
|
|
renderItem = (item, i) => {
|
|
if (item === null) {
|
|
return <li key={ 'sep' + i } className='dropdown__sep' />;
|
|
}
|
|
|
|
const { text, action, href = '#' } = item;
|
|
|
|
return (
|
|
<li className='dropdown__content-list-item' key={ text + i }>
|
|
<a href={href} target='_blank' rel='noopener' onClick={this.handleClick} data-index={i} className='dropdown__content-list-link'>
|
|
{text}
|
|
</a>
|
|
</li>
|
|
);
|
|
}
|
|
|
|
render () {
|
|
const { icon, items, size, direction, ariaLabel } = this.props;
|
|
const { expanded } = this.state;
|
|
const directionClass = (direction === "left") ? "dropdown__left" : "dropdown__right";
|
|
|
|
const dropdownItems = expanded && (
|
|
<ul className='dropdown__content-list'>
|
|
{items.map(this.renderItem)}
|
|
</ul>
|
|
);
|
|
|
|
return (
|
|
<Dropdown ref={this.setRef} onShow={this.handleShow} onHide={this.handleHide}>
|
|
<DropdownTrigger className='icon-button' style={{ fontSize: `${size}px`, width: `${size}px`, lineHeight: `${size}px` }} aria-label={ariaLabel}>
|
|
<i className={ `fa fa-fw fa-${icon} dropdown__icon` } aria-hidden={true} />
|
|
</DropdownTrigger>
|
|
|
|
<DropdownContent className={directionClass}>
|
|
{dropdownItems}
|
|
</DropdownContent>
|
|
</Dropdown>
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
export default DropdownMenu;
|