import classNames from 'clsx'; import { supportsPassiveEvents } from 'detect-passive-events'; import React, { useState, useRef, useEffect } from 'react'; import { useIntl, defineMessages } from 'react-intl'; import { spring } from 'react-motion'; // @ts-ignore import Overlay from 'react-overlays/lib/Overlay'; import { changeComposeVisibility } from 'soapbox/actions/compose'; import { closeModal, openModal } from 'soapbox/actions/modals'; import Icon from 'soapbox/components/icon'; import { IconButton } from 'soapbox/components/ui'; import { useAppDispatch, useCompose } from 'soapbox/hooks'; import { isUserTouching } from 'soapbox/is-mobile'; import Motion from '../../ui/util/optional-motion'; const messages = defineMessages({ public_short: { id: 'privacy.public.short', defaultMessage: 'Public' }, public_long: { id: 'privacy.public.long', defaultMessage: 'Post to public timelines' }, unlisted_short: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' }, unlisted_long: { id: 'privacy.unlisted.long', defaultMessage: 'Do not post to public timelines' }, private_short: { id: 'privacy.private.short', defaultMessage: 'Followers-only' }, private_long: { id: 'privacy.private.long', defaultMessage: 'Post to followers only' }, direct_short: { id: 'privacy.direct.short', defaultMessage: 'Direct' }, direct_long: { id: 'privacy.direct.long', defaultMessage: 'Post to mentioned users only' }, change_privacy: { id: 'privacy.change', defaultMessage: 'Adjust post privacy' }, }); const listenerOptions = supportsPassiveEvents ? { passive: true } : false; interface IPrivacyDropdownMenu { style?: React.CSSProperties, items: any[], value: string, placement: string, onClose: () => void, onChange: (value: string | null) => void, unavailable?: boolean, } const PrivacyDropdownMenu: React.FC = ({ style, items, placement, value, onClose, onChange }) => { const node = useRef(null); const focusedItem = useRef(null); const [mounted, setMounted] = useState(false); const handleDocumentClick = (e: MouseEvent | TouchEvent) => { if (node.current && !node.current.contains(e.target as HTMLElement)) { onClose(); } }; const handleKeyDown: React.KeyboardEventHandler = e => { const value = e.currentTarget.getAttribute('data-index'); const index = items.findIndex(item => item.value === value); let element: ChildNode | null | undefined = null; switch (e.key) { case 'Escape': onClose(); break; case 'Enter': handleClick(e); break; case 'ArrowDown': element = node.current?.childNodes[index + 1] || node.current?.firstChild; break; case 'ArrowUp': element = node.current?.childNodes[index - 1] || node.current?.lastChild; break; case 'Tab': if (e.shiftKey) { element = node.current?.childNodes[index - 1] || node.current?.lastChild; } else { element = node.current?.childNodes[index + 1] || node.current?.firstChild; } break; case 'Home': element = node.current?.firstChild; break; case 'End': element = node.current?.lastChild; break; } if (element) { (element as HTMLElement).focus(); onChange((element as HTMLElement).getAttribute('data-index')); e.preventDefault(); e.stopPropagation(); } }; const handleClick: React.EventHandler = (e: MouseEvent | KeyboardEvent) => { const value = (e.currentTarget as HTMLElement)?.getAttribute('data-index'); e.preventDefault(); onClose(); onChange(value); }; useEffect(() => { document.addEventListener('click', handleDocumentClick, false); document.addEventListener('touchend', handleDocumentClick, listenerOptions); focusedItem.current?.focus({ preventScroll: true }); setMounted(true); return () => { document.removeEventListener('click', handleDocumentClick, false); document.removeEventListener('touchend', handleDocumentClick); }; }, []); return ( {({ opacity, scaleX, scaleY }) => ( // It should not be transformed when mounting because the resulting // size will be used to determine the coordinate of the menu by // react-overlays
{items.map(item => (
{item.text} {item.meta}
))}
)}
); }; interface IPrivacyDropdown { composeId: string, } const PrivacyDropdown: React.FC = ({ composeId, }) => { const dispatch = useAppDispatch(); const intl = useIntl(); const node = useRef(null); const activeElement = useRef(null); const compose = useCompose(composeId); const value = compose.privacy; const unavailable = compose.id; const [open, setOpen] = useState(false); const [placement, setPlacement] = useState('bottom'); const options = [ { icon: require('@tabler/icons/world.svg'), value: 'public', text: intl.formatMessage(messages.public_short), meta: intl.formatMessage(messages.public_long) }, { icon: require('@tabler/icons/lock-open.svg'), value: 'unlisted', text: intl.formatMessage(messages.unlisted_short), meta: intl.formatMessage(messages.unlisted_long) }, { icon: require('@tabler/icons/lock.svg'), value: 'private', text: intl.formatMessage(messages.private_short), meta: intl.formatMessage(messages.private_long) }, { icon: require('@tabler/icons/mail.svg'), value: 'direct', text: intl.formatMessage(messages.direct_short), meta: intl.formatMessage(messages.direct_long) }, ]; const onChange = (value: string | null) => value && dispatch(changeComposeVisibility(composeId, value)); const onModalOpen = (props: Record) => dispatch(openModal('ACTIONS', props)); const onModalClose = () => dispatch(closeModal('ACTIONS')); const handleToggle: React.MouseEventHandler = (e) => { if (isUserTouching()) { if (open) { onModalClose(); } else { onModalOpen({ actions: options.map(option => ({ ...option, active: option.value === value })), onClick: handleModalActionClick, }); } } else { const { top } = e.currentTarget.getBoundingClientRect(); if (open) { activeElement.current?.focus(); } setPlacement(top * 2 < innerHeight ? 'bottom' : 'top'); setOpen(!open); } e.stopPropagation(); }; const handleModalActionClick: React.MouseEventHandler = (e) => { e.preventDefault(); const { value } = options[e.currentTarget.getAttribute('data-index') as any]; onModalClose(); onChange(value); }; const handleKeyDown: React.KeyboardEventHandler = e => { switch (e.key) { case 'Escape': handleClose(); break; } }; const handleMouseDown = () => { if (!open) { activeElement.current = document.activeElement as HTMLElement | null; } }; const handleButtonKeyDown: React.KeyboardEventHandler = (e) => { switch (e.key) { case ' ': case 'Enter': handleMouseDown(); break; } }; const handleClose = () => { if (open) { activeElement.current?.focus(); } setOpen(false); }; if (unavailable) { return null; } const valueOption = options.find(item => item.value === value); return (
); }; export default PrivacyDropdown;