-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
310 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Meta, StoryObj } from '@storybook/web-components'; | ||
import { html } from 'lit'; | ||
import { BTAvatarProps } from './Avatar'; | ||
|
||
const meta: Meta<BTAvatarProps> = { | ||
title: 'BTAvatar', | ||
render: () => | ||
html`<header is="bt-header"> | ||
<nav> | ||
<a href="/"><h1 is="bt-heading">App</h1></a> | ||
<section> | ||
<button is="bt-avatar" id="avatar" popovertarget="avatar-popover"> | ||
JD | ||
</button> | ||
<div popover id="avatar-popover" is="bt-popover"> | ||
<ul is="bt-dropdown-menu"> | ||
<li separated> | ||
<p>John Doe</p> | ||
<p>[email protected]</p> | ||
</li> | ||
<li><button type="button">Sign out</button></li> | ||
</ul> | ||
</div> | ||
</section> | ||
</nav> | ||
</header> | ||
<div style="min-height: 50px"></div>`, | ||
parameters: { | ||
layout: 'fullscreen', | ||
}, | ||
}; | ||
type Story = StoryObj<BTAvatarProps>; | ||
|
||
export default meta; | ||
|
||
export const Default: Story = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { css } from 'lit'; | ||
import { customElement } from '../utils'; | ||
|
||
export type BTAvatarProps = {}; | ||
|
||
@customElement({ | ||
name: 'bt-avatar', | ||
extends: 'button', | ||
styles: css` | ||
:host { | ||
position: relative; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
width: 40px; | ||
height: 40px; | ||
overflow: hidden; | ||
background-color: hsl(215, 14%, 34%); | ||
border-radius: 9999px; | ||
border: none; | ||
color: hsl(216, 12%, 84%); | ||
cursor: pointer; | ||
} | ||
`, | ||
}) | ||
export class BTAvatar extends HTMLButtonElement {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Meta, StoryObj } from '@storybook/web-components'; | ||
import { html } from 'lit'; | ||
import { BTDropdownMenuProps } from './DropdownMenu'; | ||
|
||
const meta: Meta<BTDropdownMenuProps> = { | ||
title: 'BTDropdownMenu', | ||
render: () => | ||
html`<div is="bt-popover"> | ||
<ul is="bt-dropdown-menu"> | ||
<li separated> | ||
<p>John Doe</p> | ||
<p>[email protected]</p> | ||
</li> | ||
<li><a href="#">Menu item 1</a></li> | ||
<li><a href="#">Menu item 2</a></li> | ||
<li separated><a href="#">Menu item 3</a></li> | ||
<li><button type="button">Sign out</button></li> | ||
</ul> | ||
</div>`, | ||
}; | ||
type Story = StoryObj<BTDropdownMenuProps>; | ||
|
||
export default meta; | ||
|
||
export const Default: Story = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { css } from 'lit'; | ||
import { customElement } from '../utils'; | ||
|
||
export type BTDropdownMenuProps = {}; | ||
|
||
@customElement({ | ||
name: 'bt-dropdown-menu', | ||
extends: 'ul', | ||
styles: css` | ||
:host { | ||
list-style-type: unset; | ||
margin: unset; | ||
padding: unset; | ||
li { | ||
display: block; | ||
font-size: 0.875rem; | ||
color: hsl(220, 13%, 91%); | ||
--padding: var(--padding-top, 0.5rem) 1rem var(--padding-bottom, 0.5rem); | ||
--separator-gap: calc(0.3rem + 1px); | ||
&:has(a, button):hover { | ||
background-color: hsl(215, 14%, 34%); | ||
color: var(--bt-strong-text-color); | ||
} | ||
&:not(:has(a, button)) { | ||
padding: var(--padding); | ||
} | ||
&:first-child { | ||
--padding-top: 0.75rem; | ||
} | ||
&:last-child { | ||
--padding-bottom: 0.75rem; | ||
} | ||
&[separated] { | ||
position: relative; | ||
margin-bottom: var(--separator-gap); | ||
&:after { | ||
position: absolute; | ||
height: 0.3rem; | ||
bottom: calc(-0.15rem - 1px); | ||
left: 0; | ||
right: 0; | ||
border-bottom: 1px solid hsla(215, 14%, 34%); | ||
content: ''; | ||
display: block; | ||
} | ||
} | ||
[separated] + & { | ||
margin-top: var(--separator-gap); | ||
} | ||
} | ||
a, | ||
button { | ||
padding: var(--padding); | ||
} | ||
a { | ||
display: block; | ||
text-decoration: unset; | ||
color: inherit; | ||
} | ||
button { | ||
width: 100%; | ||
border: unset; | ||
background-color: unset; | ||
font: inherit; | ||
cursor: pointer; | ||
text-align: left; | ||
color: inherit; | ||
} | ||
p { | ||
margin: unset; | ||
padding: 0.2rem 0; | ||
} | ||
} | ||
`, | ||
}) | ||
export class BTDropdownMenu extends HTMLUListElement {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Meta, StoryObj } from '@storybook/web-components'; | ||
import { html } from 'lit'; | ||
import { BTPopoverProps } from './Popover'; | ||
|
||
const meta: Meta<BTPopoverProps> = { | ||
title: 'BTPopover', | ||
render: () => | ||
html`<button is="bt-button" id="trigger" popovertarget="test-popover"> | ||
Open | ||
</button> | ||
<div popover id="test-popover" is="bt-popover"> | ||
<div>Content text</div> | ||
</div> | ||
<div style="min-height: 50px"></div>`, | ||
}; | ||
type Story = StoryObj<BTPopoverProps>; | ||
|
||
export default meta; | ||
|
||
export const Default: Story = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { css } from 'lit'; | ||
import { customElement } from '../utils'; | ||
|
||
export type BTPopoverProps = {}; | ||
|
||
@customElement({ | ||
name: 'bt-popover', | ||
extends: 'div', | ||
styles: css` | ||
:host { | ||
margin: unset; | ||
padding: unset; | ||
white-space: nowrap; | ||
top: calc(var(--trigger-bottom) + 0.5rem); | ||
background-color: hsl(217, 19%, 27%); | ||
border-radius: 0.5rem; | ||
min-width: 11rem; | ||
border: none; | ||
color: var(--bt-strong-text-color); | ||
overflow: hidden; | ||
&.left { | ||
left: var(--trigger-left); | ||
} | ||
&.right { | ||
left: var(--trigger-right); | ||
transform: translateX(-100%); | ||
} | ||
&.center { | ||
left: var(--trigger-center); | ||
transform: translateX(-50%); | ||
} | ||
} | ||
`, | ||
}) | ||
export class BTPopover extends HTMLDivElement { | ||
connectedCallback() { | ||
this.addEventListener('beforetoggle', () => { | ||
this.style.opacity = '0'; | ||
}); | ||
this.addEventListener('toggle', () => { | ||
const trigger = document.querySelector( | ||
`[popovertarget="${this.id}"]`, | ||
) as HTMLElement; | ||
this.style.setProperty( | ||
'--trigger-bottom', | ||
`${trigger.getBoundingClientRect().bottom}px`, | ||
); | ||
this.style.setProperty( | ||
'--trigger-right', | ||
`${trigger.getBoundingClientRect().right}px`, | ||
); | ||
this.style.setProperty( | ||
'--trigger-left', | ||
`${trigger.getBoundingClientRect().left}px`, | ||
); | ||
this.style.setProperty( | ||
'--trigger-center', | ||
`${(trigger.getBoundingClientRect().right - trigger.getBoundingClientRect().left) / 2}px`, | ||
); | ||
const overflows = ['left', 'right', 'center'].map((position) => { | ||
this.classList.add(position); | ||
const measure = { | ||
position, | ||
overflow: Math.max( | ||
-Math.min(this.getBoundingClientRect().left, 0), | ||
-Math.min( | ||
document.body.clientWidth - this.getBoundingClientRect().right, | ||
0, | ||
), | ||
), | ||
}; | ||
this.classList.remove('left', 'right', 'center'); | ||
return measure; | ||
}); | ||
overflows.sort((a, b) => a.overflow - b.overflow); | ||
this.classList.add(overflows[0].position); | ||
this.style.opacity = '1'; | ||
}); | ||
} | ||
} |
Oops, something went wrong.