Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript support for scrim #654

Merged
merged 2 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'