mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-25 09:43:21 +01:00
* feat: add a basic mention implementation example Just a simple example to get people started when trying to implement their own mentions implementation. * feat(MentionsExample): use a floating suggestions menu. * fix(MentionsExample): update to slate 0.42
100 lines
1.8 KiB
JavaScript
100 lines
1.8 KiB
JavaScript
import React from 'react'
|
|
import ReactDOM from 'react-dom'
|
|
|
|
import styled from 'react-emotion'
|
|
|
|
const SuggestionList = styled('ul')`
|
|
background: #fff;
|
|
list-style: none;
|
|
margin: 0;
|
|
padding: 0;
|
|
position: absolute;
|
|
`
|
|
|
|
const Suggestion = styled('li')`
|
|
align-items: center;
|
|
border-left: 1px solid #ddd;
|
|
border-right: 1px solid #ddd;
|
|
border-top: 1px solid #ddd;
|
|
|
|
display: flex;
|
|
height: 32px;
|
|
padding: 4px 8px;
|
|
|
|
&:hover {
|
|
background: #87cefa;
|
|
}
|
|
|
|
&:last-of-type {
|
|
border-bottom: 1px solid #ddd;
|
|
}
|
|
`
|
|
|
|
const DEFAULT_POSITION = {
|
|
top: -10000,
|
|
left: -10000,
|
|
}
|
|
|
|
/**
|
|
* Suggestions is a PureComponent because we need to prevent updates when x/ y
|
|
* Are just going to be the same value. Otherwise we will update forever.
|
|
*/
|
|
|
|
class Suggestions extends React.PureComponent {
|
|
menuRef = React.createRef()
|
|
|
|
state = DEFAULT_POSITION
|
|
|
|
/**
|
|
* On update, update the menu.
|
|
*/
|
|
|
|
componentDidMount = () => {
|
|
this.updateMenu()
|
|
}
|
|
|
|
componentDidUpdate = () => {
|
|
this.updateMenu()
|
|
}
|
|
|
|
render() {
|
|
const root = window.document.getElementById('root')
|
|
|
|
return ReactDOM.createPortal(
|
|
<SuggestionList
|
|
ref={this.menuRef}
|
|
style={{
|
|
top: this.state.top,
|
|
left: this.state.left,
|
|
}}
|
|
>
|
|
{this.props.users.map(user => {
|
|
return (
|
|
<Suggestion key={user.id} onClick={() => this.props.onSelect(user)}>
|
|
{user.username}
|
|
</Suggestion>
|
|
)
|
|
})}
|
|
</SuggestionList>,
|
|
root
|
|
)
|
|
}
|
|
|
|
updateMenu() {
|
|
const anchor = window.document.querySelector(this.props.anchor)
|
|
|
|
if (!anchor) {
|
|
return this.setState(DEFAULT_POSITION)
|
|
}
|
|
|
|
const anchorRect = anchor.getBoundingClientRect()
|
|
|
|
this.setState({
|
|
top: anchorRect.bottom,
|
|
left: anchorRect.left,
|
|
})
|
|
}
|
|
}
|
|
|
|
export default Suggestions
|