2022-06-06 17:06:25 +02:00
/ *
This file is part of GNU Taler
( C ) 2022 Taler Systems S . A .
GNU Taler is free software ; you can redistribute it and / or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation ; either version 3 , or ( at your option ) any later version .
GNU Taler is distributed in the hope that it will be useful , but WITHOUT ANY
WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE . See the GNU General Public License for more details .
You should have received a copy of the GNU General Public License along with
GNU Taler ; see the file COPYING . If not , see < http : / / www.gnu.org / licenses / >
* /
2022-03-09 18:00:02 +01:00
import { css } from "@linaria/core" ;
2022-03-11 06:17:40 +01:00
import { h , JSX , VNode , ComponentChildren } from "preact" ;
2022-03-29 04:41:07 +02:00
// eslint-disable-next-line import/extensions
2022-03-09 18:00:02 +01:00
import { alpha } from "./colors/manipulation" ;
2022-03-29 04:41:07 +02:00
// eslint-disable-next-line import/extensions
2022-03-09 18:00:02 +01:00
import { theme } from "./style" ;
const borderVariant = {
outlined : css `
border : 1px solid $ { theme . palette . divider } ;
` ,
elevation : css `
box - shadow : var ( -- theme - shadow - elevation ) ;
` ,
} ;
const baseStyle = css `
background - color : $ { theme . palette . background . paper } ;
color : $ { theme . palette . text . primary } ;
. theme - dark & {
background - image : var ( -- gradient - white - elevation ) ;
}
` ;
2022-03-11 06:17:40 +01:00
interface Props extends JSX . HTMLAttributes < HTMLDivElement > {
elevation? : number ;
square? : boolean ;
variant ? : "elevation" | "outlined" ;
children? : ComponentChildren ;
}
2022-03-09 18:00:02 +01:00
export function Paper ( {
elevation = 1 ,
square ,
variant = "elevation" ,
children ,
2022-04-05 17:16:09 +02:00
class : _class ,
style ,
2022-03-11 06:17:40 +01:00
. . . rest
} : Props ) : VNode {
2022-03-09 18:00:02 +01:00
return (
< div
class = { [
2022-04-05 17:16:09 +02:00
_class ,
2022-03-09 18:00:02 +01:00
baseStyle ,
2022-03-11 03:13:10 +01:00
! square && theme . shape . roundBorder ,
2022-03-09 18:00:02 +01:00
borderVariant [ variant ] ,
] . join ( " " ) }
style = { {
"--theme-shadow-elevation" : theme . shadows [ elevation ] ,
"--gradient-white-elevation" : ` linear-gradient( ${ alpha (
"#fff" ,
getOverlayAlpha ( elevation ) ,
) } , $ { alpha ( "#fff" , getOverlayAlpha ( elevation ) ) } ) ` ,
2022-04-05 17:16:09 +02:00
. . . ( style as any ) ,
2022-03-09 18:00:02 +01:00
} }
2022-03-11 06:17:40 +01:00
{ . . . rest }
2022-03-09 18:00:02 +01:00
>
{ children }
< / div >
) ;
}
// Inspired by https://github.com/material-components/material-components-ios/blob/bca36107405594d5b7b16265a5b0ed698f85a5ee/components/Elevation/src/UIColor%2BMaterialElevation.m#L61
2022-03-29 04:41:07 +02:00
function getOverlayAlpha ( elevation : number ) : number {
2022-03-09 18:00:02 +01:00
let alphaValue ;
if ( elevation < 1 ) {
alphaValue = 5.11916 * elevation * * 2 ;
} else {
alphaValue = 4.5 * Math . log ( elevation + 1 ) + 2 ;
}
return Number ( ( alphaValue / 100 ) . toFixed ( 2 ) ) ;
2022-03-29 04:41:07 +02:00
}