2019-10-14 00:32:11 +03:00
|
|
|
/*
|
2019-10-20 12:13:42 +03:00
|
|
|
Copyright 2019 Tulir Asokan <tulir@maunium.net>
|
2019-10-14 00:32:11 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-05-25 16:59:03 +03:00
|
|
|
import classNames from "classnames";
|
2019-10-14 00:32:11 +03:00
|
|
|
|
2020-05-25 17:46:44 +03:00
|
|
|
import {_t} from "../../../languageHandler";
|
|
|
|
import {Key} from "../../../Keyboard";
|
|
|
|
|
2019-10-14 20:14:40 +03:00
|
|
|
class Header extends React.PureComponent {
|
2019-10-14 00:32:11 +03:00
|
|
|
static propTypes = {
|
|
|
|
categories: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
|
|
onAnchorClick: PropTypes.func.isRequired,
|
2020-05-25 17:46:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
findNearestEnabled(index, delta) {
|
|
|
|
index += this.props.categories.length;
|
|
|
|
const cats = [...this.props.categories, ...this.props.categories, ...this.props.categories];
|
|
|
|
|
|
|
|
while (index < cats.length && index >= 0) {
|
|
|
|
if (cats[index].enabled) return index % this.props.categories.length;
|
|
|
|
index += delta > 0 ? 1 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changeCategoryRelative(delta) {
|
|
|
|
const current = this.props.categories.findIndex(c => c.visible);
|
|
|
|
this.changeCategoryAbsolute(current + delta, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
changeCategoryAbsolute(index, delta=1) {
|
|
|
|
const category = this.props.categories[this.findNearestEnabled(index, delta)];
|
|
|
|
if (category) {
|
|
|
|
this.props.onAnchorClick(category.id);
|
|
|
|
category.ref.current.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements ARIA Tabs with Automatic Activation pattern
|
|
|
|
// https://www.w3.org/TR/wai-aria-practices/examples/tabs/tabs-1/tabs.html
|
|
|
|
onKeyDown = (ev) => {
|
|
|
|
let handled = true;
|
|
|
|
switch (ev.key) {
|
|
|
|
case Key.ARROW_LEFT:
|
|
|
|
this.changeCategoryRelative(-1);
|
|
|
|
break;
|
|
|
|
case Key.ARROW_RIGHT:
|
|
|
|
this.changeCategoryRelative(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Key.HOME:
|
|
|
|
this.changeCategoryAbsolute(0);
|
|
|
|
break;
|
|
|
|
case Key.END:
|
|
|
|
this.changeCategoryAbsolute(this.props.categories.length - 1, -1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
handled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handled) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
2019-10-14 00:32:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-05-25 17:46:44 +03:00
|
|
|
<nav className="mx_EmojiPicker_header" role="tablist" aria-label={_t("Categories")} onKeyDown={this.onKeyDown}>
|
2020-05-25 16:59:03 +03:00
|
|
|
{this.props.categories.map(category => {
|
|
|
|
const classes = classNames(`mx_EmojiPicker_anchor mx_EmojiPicker_anchor_${category.id}`, {
|
|
|
|
mx_EmojiPicker_anchor_visible: category.visible,
|
|
|
|
});
|
2020-05-25 17:46:44 +03:00
|
|
|
// Properties of this button are also modified by EmojiPicker's updateVisibility in DOM.
|
2020-05-25 16:59:03 +03:00
|
|
|
return <button
|
|
|
|
disabled={!category.enabled}
|
|
|
|
key={category.id}
|
|
|
|
ref={category.ref}
|
|
|
|
className={classes}
|
|
|
|
onClick={() => this.props.onAnchorClick(category.id)}
|
|
|
|
title={category.name}
|
|
|
|
role="tab"
|
2020-05-25 17:46:44 +03:00
|
|
|
tabIndex={category.visible ? 0 : -1} // roving
|
2020-05-25 16:59:03 +03:00
|
|
|
aria-selected={category.visible}
|
|
|
|
aria-controls={`mx_EmojiPicker_category_${category.id}`}
|
|
|
|
/>;
|
|
|
|
})}
|
2019-10-14 00:32:11 +03:00
|
|
|
</nav>
|
2019-10-20 12:13:32 +03:00
|
|
|
);
|
2019-10-14 00:32:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Header;
|