Skip to content

Instantly share code, notes, and snippets.

@e111077
Last active August 10, 2023 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e111077/3ee0643323ce76d53bb8f5a98149dd45 to your computer and use it in GitHub Desktop.
Save e111077/3ee0643323ce76d53bb8f5a98149dd45 to your computer and use it in GitHub Desktop.
Material Elevation
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.0.0-beta.69/dist/themes/light.css">
<script type="module" src="./material-box-shadow.js"></script>
<style>
:root {
font-family: Roboto, sans-serif;
}
</style>
</head>
<body>
<h1>Material 2 Box Shadow Generator</h1>
<material-box-shadow></material-box-shadow>
</body>
import {html, css, LitElement} from 'lit';
import {customElement, property, state, query} from 'lit/decorators.js';
import '@shoelace-style/shoelace/dist/components/menu/menu.js';
import '@shoelace-style/shoelace/dist/components/select/select.js';
import '@shoelace-style/shoelace/dist/components/option/option.js';
@customElement('material-box-shadow')
export class MaterialBoxShadow extends LitElement {
static styles = css`
#wrapper {
transition: box-shadow 200ms ease-in;
width: 500px;
height: 250px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: white;
border-radius: 4px;
}`;
@property() elevation: string = '0';
@state() boxShadow = 'rgb(0,0,0) 0px 0px 0px;';
render() {
return html`
<div id="wrapper" style=${`box-shadow: ${this.boxShadow};`}>
<sl-select label="Elevation" .value=${this.elevation} @sl-change=${this.onChange}>
${Object.keys(umbraMap).map(elevation => html`
<sl-option value=${elevation}>${elevation}</sl-option>`)}
</sl-select>
<div>
${this.elevation !== '' ? html`<code>box-shadow: ${this.boxShadow};</code>`: ''}
</div>
</div<`;
}
private onChange(e: Event) {
this.elevation = (e.target as HTMLSelectElement).value ?? '0';
this.boxShadow = `rgba(0, 0, 0, ${umbraOpacity}) ${umbraMap[this.elevation]}, rgba(0, 0, 0, ${penumbraOpacity}) ${penumbraMap[this.elevation]}, rgba(0, 0, 0, ${ambientOpacity}) ${ambientMap[this.elevation]}`
}
}
const umbraOpacity = 0.2;
const penumbraOpacity = 0.14;
const ambientOpacity = 0.12;
const umbraMap = {
0: '0px 0px 0px 0px',
1: '0px 2px 1px -1px',
2: '0px 3px 1px -2px',
3: '0px 3px 3px -2px',
4: '0px 2px 4px -1px',
5: '0px 3px 5px -1px',
6: '0px 3px 5px -1px',
7: '0px 4px 5px -2px',
8: '0px 5px 5px -3px',
9: '0px 5px 6px -3px',
10: '0px 6px 6px -3px',
11: '0px 6px 7px -4px',
12: '0px 7px 8px -4px',
13: '0px 7px 8px -4px',
14: '0px 7px 9px -4px',
15: '0px 8px 9px -5px',
16: '0px 8px 10px -5px',
17: '0px 8px 11px -5px',
18: '0px 9px 11px -5px',
19: '0px 9px 12px -6px',
20: '0px 10px 13px -6px',
21: '0px 10px 13px -6px',
22: '0px 10px 14px -6px',
23: '0px 11px 14px -7px',
24: '0px 11px 15px -7px',
};
const penumbraMap = {
0: '0px 0px 0px 0px',
1: '0px 1px 1px 0px',
2: '0px 2px 2px 0px',
3: '0px 3px 4px 0px',
4: '0px 4px 5px 0px',
5: '0px 5px 8px 0px',
6: '0px 6px 10px 0px',
7: '0px 7px 10px 1px',
8: '0px 8px 10px 1px',
9: '0px 9px 12px 1px',
10: '0px 10px 14px 1px',
11: '0px 11px 15px 1px',
12: '0px 12px 17px 2px',
13: '0px 13px 19px 2px',
14: '0px 14px 21px 2px',
15: '0px 15px 22px 2px',
16: '0px 16px 24px 2px',
17: '0px 17px 26px 2px',
18: '0px 18px 28px 2px',
19: '0px 19px 29px 2px',
20: '0px 20px 31px 3px',
21: '0px 21px 33px 3px',
22: '0px 22px 35px 3px',
23: '0px 23px 36px 3px',
24: '0px 24px 38px 3px',
};
const ambientMap = {
0: '0px 0px 0px 0px',
1: '0px 1px 3px 0px',
2: '0px 1px 5px 0px',
3: '0px 1px 8px 0px',
4: '0px 1px 10px 0px',
5: '0px 1px 14px 0px',
6: '0px 1px 18px 0px',
7: '0px 2px 16px 1px',
8: '0px 3px 14px 2px',
9: '0px 3px 16px 2px',
10: '0px 4px 18px 3px',
11: '0px 4px 20px 3px',
12: '0px 5px 22px 4px',
13: '0px 5px 24px 4px',
14: '0px 5px 26px 4px',
15: '0px 6px 28px 5px',
16: '0px 6px 30px 5px',
17: '0px 6px 32px 5px',
18: '0px 7px 34px 6px',
19: '0px 7px 36px 6px',
20: '0px 8px 38px 7px',
21: '0px 8px 40px 7px',
22: '0px 8px 42px 7px',
23: '0px 9px 44px 8px',
24: '0px 9px 46px 8px',
};
{
"dependencies": {
"lit": "^2.0.0",
"@lit/reactive-element": "^1.0.0",
"lit-element": "^3.0.0",
"lit-html": "^2.0.0"
}
}
{
"files": {
"material-box-shadow.ts": {
"position": 0
},
"index.html": {
"position": 1
},
"package.json": {
"position": 2,
"hidden": true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment