-
-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract toggle button in list block (#8795)
- Loading branch information
Showing
9 changed files
with
311 additions
and
141 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
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,7 @@ | ||
import { TOGGLE_BUTTON_PARENT_CLASS, ToggleButton } from './toggle-button.js'; | ||
|
||
export function effects() { | ||
customElements.define('blocksuite-toggle-button', ToggleButton); | ||
} | ||
|
||
export { TOGGLE_BUTTON_PARENT_CLASS, ToggleButton }; |
82 changes: 82 additions & 0 deletions
82
packages/affine/components/src/toggle-button/toggle-button.ts
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,82 @@ | ||
import { ShadowlessElement } from '@blocksuite/block-std'; | ||
import { WithDisposable } from '@blocksuite/global/utils'; | ||
import { css, unsafeCSS } from 'lit'; | ||
import { property } from 'lit/decorators.js'; | ||
import { html } from 'lit-html'; | ||
|
||
import { toggleDown, toggleRight } from '../icons/list.js'; | ||
|
||
export const TOGGLE_BUTTON_PARENT_CLASS = 'blocksuite-toggle-button-parent'; | ||
|
||
export class ToggleButton extends WithDisposable(ShadowlessElement) { | ||
static override styles = css` | ||
.toggle-icon { | ||
display: flex; | ||
align-items: center; | ||
height: 16px; | ||
margin: 4px 0; | ||
position: absolute; | ||
left: 0; | ||
transform: translateX(-100%); | ||
border-radius: 4px; | ||
cursor: pointer; | ||
opacity: 0; | ||
transition: opacity 0.2s ease-in-out; | ||
} | ||
.toggle-icon:hover { | ||
background: var(--affine-hover-color); | ||
} | ||
.toggle-icon[data-collapsed='true'] { | ||
opacity: 1; | ||
} | ||
.${unsafeCSS(TOGGLE_BUTTON_PARENT_CLASS)}:hover .toggle-icon { | ||
opacity: 1; | ||
} | ||
.with-drag-handle .toggle-icon { | ||
opacity: 1; | ||
} | ||
.with-drag-handle .affine-block-children-container .toggle-icon { | ||
opacity: 0; | ||
} | ||
`; | ||
|
||
override render() { | ||
const toggleDownTemplate = html` | ||
<div | ||
contenteditable="false" | ||
class="toggle-icon" | ||
@click=${() => this.updateCollapsed(!this.collapsed)} | ||
> | ||
${toggleDown} | ||
</div> | ||
`; | ||
|
||
const toggleRightTemplate = html` | ||
<div | ||
contenteditable="false" | ||
class="toggle-icon" | ||
data-collapsed=${this.collapsed} | ||
@click=${() => this.updateCollapsed(!this.collapsed)} | ||
> | ||
${toggleRight} | ||
</div> | ||
`; | ||
|
||
return this.collapsed ? toggleRightTemplate : toggleDownTemplate; | ||
} | ||
|
||
@property({ attribute: false }) | ||
accessor collapsed!: boolean; | ||
|
||
@property({ attribute: false }) | ||
accessor updateCollapsed!: (collapsed: boolean) => void; | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'blocksuite-toggle-button': ToggleButton; | ||
} | ||
} |
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
Oops, something went wrong.