Skip to content

Commit

Permalink
confirm width state on mouse up
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix22 committed Jun 13, 2022
1 parent 33cc091 commit bd8cb2a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions components/Carbon.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,11 @@ class Carbon extends React.PureComponent {
<div className="bg" />
</div>

{/* TODO define innerRef in Editor, and pass in this child as a prop to Carbon */}
<WidthHandler
innerRef={this.props.innerRef}
onChange={this.props.updateWidth}
onConfirm={this.props.confirmWidth}
paddingHorizontal={config.paddingHorizontal}
/>
</div>
Expand Down
2 changes: 2 additions & 0 deletions components/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Editor extends React.Component {
updateCode = code => this.updateState({ code })
updateTitleBar = titleBar => this.updateState({ titleBar })
updateWidth = width => this.setState({ widthAdjustment: false, width })
confirmWidth = () => this.onUpdate(this.state)

getCarbonImage = async (
{
Expand Down Expand Up @@ -399,6 +400,7 @@ class Editor extends React.Component {
config={this.state}
onChange={this.updateCode}
updateWidth={this.updateWidth}
confirmWidth={this.confirmWidth}
loading={this.state.loading}
theme={theme}
titleBar={titleBar}
Expand Down
16 changes: 10 additions & 6 deletions components/WidthHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ function clamp(value, min, max) {
return value
}

export default function WidthHandler(props) {
const { onChange, innerRef, paddingHorizontal } = props

export default function WidthHandler({ onChange, onConfirm, innerRef, paddingHorizontal }) {
const startX = React.useRef(null)
const startWidth = React.useRef(null)

Expand All @@ -34,6 +32,15 @@ export default function WidthHandler(props) {
return () => window.removeEventListener('mousemove', handleMouseMove)
}, [innerRef, onChange])

React.useEffect(() => {
function handleMouseUp() {
startX.current = null
onConfirm()
}
window.addEventListener('mouseup', handleMouseUp)
return () => window.removeEventListener('mouseup', handleMouseUp)
}, [onConfirm])

return (
// eslint-disable-next-line
<div
Expand All @@ -42,9 +49,6 @@ export default function WidthHandler(props) {
startX.current = e.pageX
startWidth.current = innerRef.current.clientWidth
}}
onMouseUp={() => {
startX.current = null
}}
role="separator"
aria-orientation="vertical"
aria-valuemin={minWidth}
Expand Down

0 comments on commit bd8cb2a

Please sign in to comment.