forked from uber/baseweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(layer): implement keyboard event handlers (uber#5080)
* feat(layer): implement keyboard event handlers * feat(layer): implement keyboard event handlers * test(vrt): update visual snapshots for 22b9052 [skip ci] Co-authored-by: Chase Starr <[email protected]> Co-authored-by: UberOpenSourceBot <[email protected]>
- Loading branch information
1 parent
0a7fa59
commit 7694aa9
Showing
19 changed files
with
442 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
Copyright (c) Uber Technologies, Inc. | ||
This source code is licensed under the MIT license found in the | ||
LICENSE file in the root directory of this source tree. | ||
*/ | ||
import * as React from 'react'; | ||
import { Layer, TetherBehavior, TETHER_PLACEMENT } from '..'; | ||
import { Block } from '../../block'; | ||
import { Button } from '../../button'; | ||
import type { NormalizedOffsets } from '../types'; | ||
|
||
function BlockComponent(props) { | ||
const { children, forwardedRef, offset, color, ...restProps } = props; | ||
return ( | ||
<Block | ||
ref={forwardedRef} | ||
position="absolute" | ||
top={`${offset.top}px` || '50%'} | ||
left={`${offset.left}px` || '50%'} | ||
width="200px" | ||
paddingTop="20px" | ||
paddingBottom="20px" | ||
paddingLeft="20px" | ||
paddingRight="20px" | ||
backgroundColor={color} | ||
overrides={{ | ||
Block: { | ||
style: { | ||
textAlign: 'center', | ||
}, | ||
}, | ||
}} | ||
{...restProps} | ||
> | ||
{children} | ||
</Block> | ||
); | ||
} | ||
|
||
export class Scenario extends React.Component< | ||
{}, | ||
{ | ||
isFirstOpen: boolean; | ||
isSecondOpen: boolean; | ||
isFirstMounted: boolean; | ||
isSecondMounted: boolean; | ||
offset1: { | ||
top: number; | ||
left: number; | ||
}; | ||
offset2: { | ||
top: number; | ||
left: number; | ||
}; | ||
} | ||
> { | ||
anchorRef1 = React.createRef<HTMLButtonElement>(); | ||
popperRef1 = React.createRef<HTMLElement>(); | ||
anchorRef2 = React.createRef<HTMLButtonElement>(); | ||
popperRef2 = React.createRef<HTMLElement>(); | ||
|
||
state = { | ||
isFirstOpen: false, | ||
isSecondOpen: false, | ||
isFirstMounted: false, | ||
isSecondMounted: false, | ||
offset1: { top: 0, left: 0 }, | ||
offset2: { top: 0, left: 0 }, | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
onPopperUpdate = (order: 1 | 2, normalizedOffsets: NormalizedOffsets, _) => { | ||
// @ts-expect-error partial state update | ||
this.setState({ | ||
[`offset${order}`]: normalizedOffsets.popper, | ||
}); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<Block display="flex" justifyContent="flex-start" alignItems="center"> | ||
<Block> | ||
<Button ref={this.anchorRef1} onClick={() => this.setState({ isFirstOpen: true })}> | ||
Render Yellow Layer | ||
</Button> | ||
{this.state.isFirstOpen ? ( | ||
<Layer | ||
onMount={() => this.setState({ isFirstMounted: true })} | ||
onUnmount={() => this.setState({ isFirstMounted: false })} | ||
onKeyPress={(e: KeyboardEvent) => { | ||
if (e.key === 'Enter') { | ||
e.preventDefault(); | ||
this.setState({ isFirstOpen: false }); | ||
} | ||
}} | ||
> | ||
<TetherBehavior | ||
anchorRef={this.anchorRef1.current} | ||
popperRef={this.popperRef1.current} | ||
onPopperUpdate={(...args) => this.onPopperUpdate(1, ...args)} | ||
placement={TETHER_PLACEMENT.right} | ||
> | ||
<BlockComponent | ||
forwardedRef={this.popperRef1} | ||
offset={this.state.offset1} | ||
color="rgba(255, 255, 190, 0.86)" | ||
> | ||
Press "Enter" to Close | ||
</BlockComponent> | ||
</TetherBehavior> | ||
</Layer> | ||
) : null} | ||
<Block padding="5px" /> | ||
<Button ref={this.anchorRef2} onClick={() => this.setState({ isSecondOpen: true })}> | ||
Render Green Layer | ||
</Button> | ||
{this.state.isSecondOpen ? ( | ||
<Layer | ||
onMount={() => this.setState({ isSecondMounted: true })} | ||
onUnmount={() => this.setState({ isSecondMounted: false })} | ||
onKeyPress={(e: KeyboardEvent) => { | ||
if (e.key === 'Enter') { | ||
e.preventDefault(); | ||
this.setState({ isSecondOpen: false }); | ||
} | ||
}} | ||
> | ||
<TetherBehavior | ||
anchorRef={this.anchorRef2.current} | ||
popperRef={this.popperRef2.current} | ||
onPopperUpdate={(...args) => this.onPopperUpdate(2, ...args)} | ||
placement={TETHER_PLACEMENT.right} | ||
> | ||
<BlockComponent | ||
forwardedRef={this.popperRef2} | ||
offset={this.state.offset2} | ||
color="rgba(190, 255, 190, 0.86)" | ||
> | ||
Press "Enter" to Close | ||
</BlockComponent> | ||
</TetherBehavior> | ||
</Layer> | ||
) : null} | ||
</Block> | ||
</Block> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.