-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Typescript support for scrim #560 * Removed duplicate types declaration
- Loading branch information
Showing
5 changed files
with
82 additions
and
92 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
1 change: 0 additions & 1 deletion
1
...ries/core-react/src/Scrim/Scrim.tokens.js → ...ries/core-react/src/Scrim/Scrim.tokens.ts
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// @ts-nocheck | ||
import { tokens } from '@equinor/eds-tokens' | ||
|
||
const { | ||
|
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,79 @@ | ||
import React, { forwardRef, useEffect, MouseEvent } from 'react' | ||
import styled from 'styled-components' | ||
import { scrim as tokens } from './Scrim.tokens' | ||
|
||
const { height, width, background } = tokens | ||
|
||
type Props = { | ||
/** Whether scrim can be dismissed with esc key */ | ||
isDismissable?: boolean | ||
/** function to handle closing scrim */ | ||
onClose?: (event: MouseEvent | KeyboardEvent, open: boolean) => void | ||
} & React.HTMLAttributes<HTMLElement> | ||
|
||
const StyledScrim = styled.div<Props>` | ||
width: ${width}; | ||
height: ${height}; | ||
background: ${background}; | ||
position: fixed; | ||
z-index: 11; | ||
top: 0; | ||
left: 0; | ||
align-items: center; | ||
justify-content: center; | ||
display: flex; | ||
` | ||
|
||
const ScrimContent = styled.div` | ||
width: auto; | ||
height: auto; | ||
` | ||
|
||
export const Scrim = forwardRef<HTMLDivElement, Props>(function EdsScrim( | ||
{ children, onClose, isDismissable = false, ...rest }, | ||
ref, | ||
) { | ||
const handleKeyboardClose = (event: KeyboardEvent) => { | ||
if (event) { | ||
if (event.key === 'Escape' && isDismissable) { | ||
onClose && onClose(event, false) | ||
} | ||
} | ||
} | ||
|
||
const handleMouseClose = (event: MouseEvent) => { | ||
if (event) { | ||
if (event.type === 'click' && isDismissable) { | ||
onClose && onClose(event, false) | ||
} | ||
} | ||
} | ||
|
||
const handleContentClick = (event: MouseEvent) => { | ||
// Avoid event bubbling inside dialog/content inside scrim | ||
event.stopPropagation() | ||
} | ||
|
||
useEffect(() => { | ||
if (isDismissable) { | ||
document.addEventListener('keydown', handleKeyboardClose, false) | ||
} | ||
|
||
return () => { | ||
document.removeEventListener('keydown', handleKeyboardClose, false) | ||
} | ||
}, []) | ||
|
||
return ( | ||
<StyledScrim | ||
onClick={handleMouseClose} | ||
isDismissable={isDismissable} | ||
{...rest} | ||
ref={ref} | ||
> | ||
<ScrimContent onClick={handleContentClick}>{children}</ScrimContent> | ||
</StyledScrim> | ||
) | ||
}) | ||
|
||
Scrim.displayName = 'Scrim' |
1 change: 0 additions & 1 deletion
1
libraries/core-react/src/Scrim/index.jsx → libraries/core-react/src/Scrim/index.ts
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
// @ts-nocheck | ||
export { Scrim } from './Scrim' |