🍭 Web 上的亚克力效果
组件库以 @material-ui 为基础,也基本上只是包含一些悬浮层的组件,能看出来效果:
(请使用 Chrome V76+;从左到右为各个效果叠加的过程,源码在最下面):
Hello World
blur: 30px;
brightness: 120%
color: hsl(50, 50%, 50%)
color-saturate: 80%
color-opacity: 0.2%
noise-opacity: 0.8%
noise-scale: 30%
brightness: 120%
color: hsl(50, 50%, 50%)
color-saturate: 80%
color-opacity: 0.2%
noise-opacity: 0.8%
noise-scale: 30%
This is an error message!
This is a warning message!
This is an information message!
This is a success message!
Hide Code
import React from 'react';import { AcrylicAlert } from './acrylic-ui'export const AcrylicDemo2 = () => {return (<div><Button onClick={handleClick}>打开消息条</Button><Snackbar open={open} autoHideDuration={6000} onClose={handleClose}><AcrylicAlert onClose={handleClose} variant="filled" severity="success">This is a success message!</AcrylicAlert></Snackbar><AcrylicAlert variant="filled" severity="error">This is an error message!</AcrylicAlert><AcrylicAlert variant="filled" severity="warning">This is a warning message!</AcrylicAlert><AcrylicAlert variant="filled" severity="info">This is an information message!</AcrylicAlert><AcrylicAlert variant="filled" severity="success">This is a success message!</AcrylicAlert></div>);}
Copy
Hide Code
import React from 'react';import { AcrylicTooltip } from './acrylic-ui'export const AcrylicDemo1 = () => {return (<AcrylicTooltip title="提示文字、提示内容、BlaBla..."><Button>你好,世界!</Button></AcrylicTooltip>);}
Copy
Hide Code
import React from 'react';import { AcrylicTooltip } from './acrylic-ui'export const AcrylicDemo1 = () => {const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null);const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {setAnchorEl(event.currentTarget);};const handleClose = () => setAnchorEl(null);const open = Boolean(anchorEl);return (<div><Button onClick={handleClick}>你好,世界!</Button><AcrylicPopover open={open} anchorEl={anchorEl} onClose={handleClose}><div><div><CheckOutlinedIcon fontSize="large" /></div><div><Button fullWidth>OK</Button></div></div></AcrylicPopover></div>);}
Copy
Hide Code
import React from 'react';import { AcrylicMenu } from './acrylic-ui'export const AcrylicDemo1 = () => {const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {setAnchorEl(event.currentTarget);};const handleClose = () => setAnchorEl(null);return (<div><Button onClick={handleClick}>你好,世界!</Button><AcrylicMenu anchorEl={anchorEl} keepMounted open={Boolean(anchorEl)} onClose={handleClose}><MenuItem onClick={handleClose}>Profile</MenuItem><MenuItem onClick={handleClose}>My account</MenuItem><MenuItem onClick={handleClose}>Logout</MenuItem></AcrylicMenu></div>);}
Copy
Hide Code
import React from 'react';import { AcrylicDialog } from './acrylic-ui'export const AcrylicDemo1 = () => {const [open, setOpen] = React.useState(false);const handleClickOpen = () => setOpen(true);const handleClose = () => setOpen(false);return (<div><Button onClick={handleClickOpen}>你好,世界!</Button><AcrylicDialog open={open} onClose={handleClose}><DialogTitle>{"Use Google's location service?"}</DialogTitle><DialogContent><DialogContentText>Let Google help apps determine location. This means sending anonymous location data toGoogle, even when no apps are running.</DialogContentText></DialogContent><DialogActions><Button onClick={handleClose} color="primary">Disagree</Button><Button onClick={handleClose} color="primary" autoFocus>Agree</Button></DialogActions></AcrylicDialog></div>);}
Copy
acrylic-ui.tsx
import { withStyles } from '@material-ui/core/styles';import { CSSProperties } from '@material-ui/core/styles/withStyles';import Alert from '@material-ui/lab/Alert';import Tooltip from '@material-ui/core/Tooltip';import Popover from '@material-ui/core/Popover';import Menu from '@material-ui/core/Menu';import Dialog from '@material-ui/core/Dialog';const acrylicStyle: CSSProperties = {backdropFilter: 'blur(30px) brightness(80%) saturate(160%)',}export const AcrylicAlert = withStyles({root: {...acrylicStyle,},filledError: {backgroundColor: 'rgba(244, 67, 54, .4)',},filledWarning: {backgroundColor: 'rgba(255, 152, 0, .4)',},filledInfo: {backgroundColor: 'rgba(33, 150, 243, .4)',},filledSuccess: {backgroundColor: 'rgba(76, 175, 80, .4)',}})(Alert);export const AcrylicTooltip = withStyles({popper: {willChange: 'auto !important' // transform 会影响 back-drop},tooltip: {...acrylicStyle,backgroundColor: 'rgba(97, 97, 97, 0.5)'},})(Tooltip);export const AcrylicPopover = withStyles({paper: {...acrylicStyle,backgroundColor: 'rgba(255, 255, 255, 0.5)'},})(Popover);export const AcrylicMenu = withStyles({paper: {...acrylicStyle,backgroundColor: 'rgba(255, 255, 255, 0.5)'},})(Menu);export const AcrylicDialog = withStyles({paper: {...acrylicStyle,backgroundColor: 'rgba(255, 255, 255, 0.5)'},})(Dialog);
Copy