Skip to content

Commit

Permalink
Typescript support for scrim (#654)
Browse files Browse the repository at this point in the history
* Typescript support for scrim #560

* Removed duplicate types declaration
  • Loading branch information
wenche authored and vnys committed Nov 13, 2020
1 parent cdbed7d commit 19bbed9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 92 deletions.
87 changes: 0 additions & 87 deletions libraries/core-react/src/Scrim/Scrim.jsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-undef */
import React, { useState } from 'react'
import React, { KeyboardEvent, useState } from 'react'
import { render, cleanup, screen, fireEvent } from '@testing-library/react'
import '@testing-library/jest-dom'
import 'jest-styled-components'
Expand Down Expand Up @@ -27,8 +27,8 @@ const DismissableScrim = () => {
}

return visibleScrim ? (
<Scrim onKeyDown={handleClose}>
<button type="button" onClick={() => setVisibleScrim()}>
<Scrim onClose={handleClose} isDismissable>
<button type="button" onClick={() => setVisibleScrim(false)}>
OK
</button>
</Scrim>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { tokens } from '@equinor/eds-tokens'

const {
Expand Down
79 changes: 79 additions & 0 deletions libraries/core-react/src/Scrim/Scrim.tsx
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'
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// @ts-nocheck
export { Scrim } from './Scrim'

0 comments on commit 19bbed9

Please sign in to comment.