confirm width state on mouse up

main
Mike Fix 2 years ago
parent 33cc091e7b
commit bd8cb2a66e
No known key found for this signature in database
GPG Key ID: 1D85E862314CA79F

@ -225,9 +225,11 @@ class Carbon extends React.PureComponent {
<div className="bg" /> <div className="bg" />
</div> </div>
{/* TODO define innerRef in Editor, and pass in this child as a prop to Carbon */}
<WidthHandler <WidthHandler
innerRef={this.props.innerRef} innerRef={this.props.innerRef}
onChange={this.props.updateWidth} onChange={this.props.updateWidth}
onConfirm={this.props.confirmWidth}
paddingHorizontal={config.paddingHorizontal} paddingHorizontal={config.paddingHorizontal}
/> />
</div> </div>

@ -93,6 +93,7 @@ class Editor extends React.Component {
updateCode = code => this.updateState({ code }) updateCode = code => this.updateState({ code })
updateTitleBar = titleBar => this.updateState({ titleBar }) updateTitleBar = titleBar => this.updateState({ titleBar })
updateWidth = width => this.setState({ widthAdjustment: false, width }) updateWidth = width => this.setState({ widthAdjustment: false, width })
confirmWidth = () => this.onUpdate(this.state)
getCarbonImage = async ( getCarbonImage = async (
{ {
@ -399,6 +400,7 @@ class Editor extends React.Component {
config={this.state} config={this.state}
onChange={this.updateCode} onChange={this.updateCode}
updateWidth={this.updateWidth} updateWidth={this.updateWidth}
confirmWidth={this.confirmWidth}
loading={this.state.loading} loading={this.state.loading}
theme={theme} theme={theme}
titleBar={titleBar} titleBar={titleBar}

@ -13,9 +13,7 @@ function clamp(value, min, max) {
return value return value
} }
export default function WidthHandler(props) { export default function WidthHandler({ onChange, onConfirm, innerRef, paddingHorizontal }) {
const { onChange, innerRef, paddingHorizontal } = props
const startX = React.useRef(null) const startX = React.useRef(null)
const startWidth = React.useRef(null) const startWidth = React.useRef(null)
@ -34,6 +32,15 @@ export default function WidthHandler(props) {
return () => window.removeEventListener('mousemove', handleMouseMove) return () => window.removeEventListener('mousemove', handleMouseMove)
}, [innerRef, onChange]) }, [innerRef, onChange])
React.useEffect(() => {
function handleMouseUp() {
startX.current = null
onConfirm()
}
window.addEventListener('mouseup', handleMouseUp)
return () => window.removeEventListener('mouseup', handleMouseUp)
}, [onConfirm])
return ( return (
// eslint-disable-next-line // eslint-disable-next-line
<div <div
@ -42,9 +49,6 @@ export default function WidthHandler(props) {
startX.current = e.pageX startX.current = e.pageX
startWidth.current = innerRef.current.clientWidth startWidth.current = innerRef.current.clientWidth
}} }}
onMouseUp={() => {
startX.current = null
}}
role="separator" role="separator"
aria-orientation="vertical" aria-orientation="vertical"
aria-valuemin={minWidth} aria-valuemin={minWidth}

Loading…
Cancel
Save