2022-11-16 14:40:56 -08:00
|
|
|
import React, { useRef, useState } from 'react';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
// @ts-ignore
|
|
|
|
import Overlay from 'react-overlays/lib/Overlay';
|
|
|
|
|
2023-01-09 15:11:39 -08:00
|
|
|
import ForkAwesomeIcon from 'soapbox/components/fork-awesome-icon';
|
2022-11-16 14:40:56 -08:00
|
|
|
|
|
|
|
import IconPickerMenu from './icon-picker-menu';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
emoji: { id: 'icon_button.label', defaultMessage: 'Select icon' },
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IIconPickerDropdown {
|
2023-02-15 13:26:27 -08:00
|
|
|
value: string
|
2023-02-28 09:04:24 -08:00
|
|
|
onPickIcon: (icon: string) => void
|
2022-11-16 14:40:56 -08:00
|
|
|
}
|
|
|
|
|
2023-02-28 09:04:24 -08:00
|
|
|
const IconPickerDropdown: React.FC<IIconPickerDropdown> = ({ value, onPickIcon }) => {
|
2022-11-16 14:40:56 -08:00
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const [active, setActive] = useState(false);
|
|
|
|
const [placement, setPlacement] = useState<'bottom' | 'top'>();
|
|
|
|
|
|
|
|
const target = useRef(null);
|
|
|
|
|
|
|
|
const onShowDropdown: React.KeyboardEventHandler<HTMLDivElement> = ({ target }) => {
|
|
|
|
setActive(true);
|
|
|
|
|
|
|
|
const { top } = (target as any).getBoundingClientRect();
|
|
|
|
setPlacement(top * 2 < innerHeight ? 'bottom' : 'top');
|
|
|
|
};
|
|
|
|
|
|
|
|
const onHideDropdown = () => {
|
|
|
|
setActive(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onToggle: React.KeyboardEventHandler<HTMLDivElement> = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (!e.key || e.key === 'Enter') {
|
|
|
|
if (active) {
|
|
|
|
onHideDropdown();
|
|
|
|
} else {
|
|
|
|
onShowDropdown(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown: React.KeyboardEventHandler<HTMLDivElement> = (e) => {
|
|
|
|
if (e.key === 'Escape') {
|
|
|
|
onHideDropdown();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const title = intl.formatMessage(messages.emoji);
|
|
|
|
const forkAwesomeIcons = require('../forkawesome.json');
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div onKeyDown={handleKeyDown}>
|
|
|
|
<div
|
|
|
|
ref={target}
|
2023-02-01 14:13:42 -08:00
|
|
|
className='flex h-[38px] w-[38px] cursor-pointer items-center justify-center text-lg'
|
2022-11-16 14:40:56 -08:00
|
|
|
title={title}
|
|
|
|
aria-label={title}
|
|
|
|
aria-expanded={active}
|
|
|
|
role='button'
|
|
|
|
onClick={onToggle as any as React.MouseEventHandler<HTMLDivElement>}
|
|
|
|
onKeyDown={onToggle}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
2023-01-09 15:11:39 -08:00
|
|
|
<ForkAwesomeIcon id={value} />
|
2022-11-16 14:40:56 -08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<Overlay show={active} placement={placement} target={target.current}>
|
|
|
|
<IconPickerMenu
|
2023-02-28 09:04:24 -08:00
|
|
|
icons={forkAwesomeIcons}
|
2022-11-16 14:40:56 -08:00
|
|
|
onClose={onHideDropdown}
|
2023-02-28 09:04:24 -08:00
|
|
|
onPick={onPickIcon}
|
2022-11-16 14:40:56 -08:00
|
|
|
/>
|
|
|
|
</Overlay>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default IconPickerDropdown;
|