Newer
Older
/* eslint react/prop-types: 0 */
import React, {
useMemo,
useContext,
useState,
useEffect,
useRef,
createRef,
} from 'react';
import { WaxContext, Icon, useOnClickOutside } from 'wax-prosemirror-core';
opacity: ${props => (props.disabled ? '0.4' : '1')};
cursor: ${props => (props.disabled ? 'not-allowed' : 'pointer')};
const DropDownButton = styled.button`
background: #fff;
border: none;
color: #000;
cursor: pointer;
display: flex;
position: relative;
width: 215px;
height: 100%;
span {
position: relative;
top: 12px;
`;
const DropDownMenu = styled.div`
visibility: ${props => (props.isOpen ? 'visible' : 'hidden')};
background: #fff;
display: flex;
flex-direction: column;
border: 1px solid #ddd;
border-radius: 0.25rem;
box-shadow: 0 0.2rem 0.4rem rgb(0 0 0 / 10%);
margin: 2px auto auto;
position: absolute;
width: 220px;
max-height: 150px;
overflow-y: scroll;
z-index: 2;
span:focus {
background: #f2f9fc;
outline: 2px solid #f2f9fc;
const StyledIcon = styled(Icon)`
height: 18px;
width: 18px;
margin-left: auto;
position: relative;
top: 10px;
`;
const DropDownComponent = ({ view, tools }) => {
value: '6',
item: tools[6],
},
{
value: '7',
item: tools[7],
},
const context = useContext(WaxContext);
const {
activeView,
activeViewId,
pmViews: { main },
} = context;
const { state } = view;
const itemRefs = useRef([]);
const wrapperRef = useRef();
const [isOpen, setIsOpen] = useState(false);
useOnClickOutside(wrapperRef, () => setIsOpen(false));
const isEditable = main.props.editable(editable => {
return editable;
});
let isDisabled = !tools[0].select(state, activeView);
useEffect(() => {
if (isDisabled) setIsOpen(false);
}, [isDisabled]);
const openCloseMenu = () => {
if (!isDisabled) setIsOpen(!isOpen);
if (isOpen)
setTimeout(() => {
activeView.focus();
});
};
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const onKeyDown = (e, index) => {
e.preventDefault();
// arrow down
if (e.keyCode === 40) {
if (index === itemRefs.current.length - 1) {
itemRefs.current[0].current.focus();
} else {
itemRefs.current[index + 1].current.focus();
}
}
// arrow up
if (e.keyCode === 38) {
if (index === 0) {
itemRefs.current[itemRefs.current.length - 1].current.focus();
} else {
itemRefs.current[index - 1].current.focus();
}
}
// enter
if (e.keyCode === 13) {
itemRefs.current[index].current.click();
}
// ESC
if (e.keyCode === 27) {
openCloseMenu();
}
const onChange = option => {
tools[option.value].run(main, context);
openCloseMenu();
};
<DropDownButton
aria-expanded={isOpen}
aria-haspopup
onKeyDown={e => {
e.preventDefault();
if (e.keyCode === 40) {
itemRefs.current[0].current.focus();
}
}}
onMouseDown={openCloseMenu}
tabIndex="0"
type="button"
>
</DropDownButton>
<DropDownMenu isOpen={isOpen} role="menu">
{dropDownOptions.map((option, index) => {
itemRefs.current[index] = itemRefs.current[index] || createRef();
return (
<span
key={option.value}
onKeyDown={e => onKeyDown(e, index)}
ref={itemRefs.current[index]}
role="menuitem"
tabIndex="-1"
>
{option.label}
</span>
);
})}
</DropDownMenu>