Files
AFFiNE-Mirror/blocksuite/affine/components/src/toggle-switch/index.ts
T
donteatfriedrice a3b8aaff61 fix(editor): toggle switch style (#12436)
Closes: [BS-2852](https://linear.app/affine-design/issue/BS-2852/ui-bug:-toggle-的组件样式不一样了)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **Style**
  - Updated toggle switch appearance with refined colors and spacing for a more polished look.

- **Bug Fixes**
  - Improved toggle switch functionality in settings menus to ensure correct state display and interaction.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-22 06:13:25 +00:00

89 lines
1.6 KiB
TypeScript

import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
import { css, html, LitElement } from 'lit';
import { property } from 'lit/decorators.js';
const styles = css`
:host {
display: flex;
}
.switch {
height: 0;
width: 0;
visibility: hidden;
margin: 0;
}
label {
cursor: pointer;
text-indent: -9999px;
width: 38px;
height: 20px;
background: ${unsafeCSSVarV2('toggle/backgroundOff')};
display: block;
border-radius: 20px;
position: relative;
}
label:after {
content: '';
position: absolute;
top: 2px;
left: 2px;
width: 16px;
height: 16px;
background: ${unsafeCSSVarV2('toggle/foreground')};
border-radius: 16px;
transition: 0.1s;
}
label.on {
background: ${unsafeCSSVarV2('toggle/background')};
}
label.on:after {
left: calc(100% - 2px);
transform: translateX(-100%);
}
label:active:after {
width: 24px;
}
`;
export class ToggleSwitch extends LitElement {
static override styles = styles;
private _toggleSwitch() {
this.on = !this.on;
if (this.onChange) {
this.onChange(this.on);
}
}
override render() {
return html`
<label class=${this.on ? 'on' : ''}>
<input
type="checkbox"
class="switch"
?checked=${this.on}
@change=${this._toggleSwitch}
/>
</label>
`;
}
@property({ attribute: false })
accessor on = false;
@property({ attribute: false })
accessor onChange: ((on: boolean) => void) | undefined = undefined;
}
declare global {
interface HTMLElementTagNameMap {
'toggle-switch': ToggleSwitch;
}
}