-
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
1 parent
4657941
commit 4c02604
Showing
6 changed files
with
254 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,64 @@ | ||
import {SnackBarManager} from '@gecut/components'; | ||
import {SnackBarManager, gecutButton} from '@gecut/components'; | ||
import {render, html} from 'lit/html.js'; | ||
|
||
const sbm = new SnackBarManager({gapBottom: '0'}); | ||
let index = 0; | ||
const sbm = new SnackBarManager({ | ||
position: { | ||
bottom: '0px', | ||
left: '0px', | ||
right: '0px', | ||
}, | ||
}); | ||
|
||
const x = setInterval(() => { | ||
sbm.open('s-' + index++, { | ||
message: 'Hello ' + index, | ||
}); | ||
const id1 = 'id1'; | ||
const id2 = 'id2'; | ||
|
||
if (x > 5) clearInterval(x); | ||
}, 5000); | ||
sbm.connect(id1, { | ||
message: 'Hello ' + id1, | ||
close: true, | ||
}); | ||
|
||
render(html` <div class="mx-auto max-w-sm flex flex-col gap-4">${sbm.html}</div> `, document.body); | ||
sbm.connect(id2, { | ||
message: | ||
// eslint-disable-next-line max-len | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ' + | ||
id2, | ||
action: { | ||
label: 'Longer Action', | ||
}, | ||
}); | ||
|
||
render( | ||
html` | ||
<div class="mx-auto max-w-sm flex flex-col gap-4 relative h-full w-full"> | ||
${sbm.html} | ||
${gecutButton({ | ||
type: 'filled', | ||
label: 'Push 1', | ||
events: { | ||
click: () => { | ||
sbm.open(id1); | ||
}, | ||
}, | ||
})} | ||
${gecutButton({ | ||
type: 'filled', | ||
label: 'Push 2', | ||
events: { | ||
click: () => { | ||
sbm.open(id2); | ||
}, | ||
}, | ||
})} | ||
${gecutButton({ | ||
type: 'filled', | ||
label: 'Remove 1', | ||
events: { | ||
click: () => { | ||
sbm.disconnect(id1); | ||
}, | ||
}, | ||
})} | ||
</div> | ||
`, | ||
document.body, | ||
); |
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 |
---|---|---|
@@ -1,49 +1,86 @@ | ||
import {gecutContext} from '@gecut/lit-helper/directives/context.js'; | ||
import {GecutLogger} from '@gecut/logger'; | ||
import {ContextSignal} from '@gecut/signal'; | ||
import {map} from 'lit/directives/map.js'; | ||
import {styleMap} from 'lit/directives/style-map.js'; | ||
import {html} from 'lit/html.js'; | ||
|
||
import {gecutSnackBar, type SnackBarContent} from './snack-bar.js'; | ||
|
||
import {repeat} from 'lit/directives/repeat.js'; | ||
|
||
export interface SnackBarManagerContent { | ||
gapBottom: string; | ||
position?: { | ||
top?: string; | ||
bottom?: string; | ||
left?: string; | ||
right?: string; | ||
}; | ||
} | ||
|
||
export class SnackBarManager { | ||
constructor(content: SnackBarManagerContent) { | ||
this.content = content; | ||
this.snackBars.value = []; | ||
this.snackBars = {}; | ||
this._$updaterContext.value = 'update'; | ||
|
||
this.html = html` | ||
<div class="flex flex-col absolute inset-x-0" style=${styleMap(this.content?.position ?? {})}> | ||
${gecutContext(this._$updaterContext, () => | ||
map(Object.keys(this.snackBars), (k) => gecutSnackBar(this.snackBars[k])), | ||
)} | ||
</div> | ||
`; | ||
} | ||
|
||
content: SnackBarManagerContent = {}; | ||
snackBars: Record<string, ContextSignal<SnackBarContent>> = {}; | ||
html; | ||
|
||
protected _$log = new GecutLogger('gecut-snackbar-manager'); | ||
protected _$updaterContext = new ContextSignal<'update'>('gecut-snackbar-updater', 'AnimationFrame'); | ||
|
||
connect(id: string, content: SnackBarContent) { | ||
this._$log.methodArgs?.('connect', {id, content}); | ||
|
||
const context = new ContextSignal<SnackBarContent>(id, 'AnimationFrame'); | ||
context.value = {...content, open: false}; | ||
|
||
// this.html = html`${gecutContext<'open' | 'close'>(this.controller, (status) => { | ||
// const dialogContent: DialogContent = {...this.content, controller: this.controller, provider: this.provider}; | ||
this.snackBars[id] = context; | ||
this.update(); | ||
} | ||
disconnect(id: string) { | ||
this._$log.methodArgs?.('disconnect', {id}); | ||
|
||
// return gecutDialog(dialogContent, status === 'open'); | ||
// })}`; | ||
this.close(id); | ||
|
||
// this.controller.value = 'close'; | ||
setTimeout(() => { | ||
delete this.snackBars[id]; | ||
this.update(); | ||
}, 500); | ||
} | ||
|
||
content: SnackBarManagerContent; | ||
snackBars = new ContextSignal<[string, SnackBarContent, {open: true}][]>('snack-bars'); | ||
html = gecutContext(this.snackBars, (snackBars) => | ||
repeat( | ||
snackBars, | ||
(snackBar) => snackBar[0], | ||
(snackBar) => gecutSnackBar(snackBar[1]), | ||
), | ||
); | ||
|
||
open(id: string, content: SnackBarContent) { | ||
this.snackBars.functionalValue((old) => [[id, content, {open: true}], ...(old ?? [])]); | ||
open(id: string) { | ||
this._$log.methodArgs?.('open', {id}); | ||
|
||
if (!this.snackBars[id]) return this._$log.warning('open', 'id_not_found', `'${id}' not found`); | ||
|
||
this.snackBars[id].functionalValue((old) => { | ||
return {...(old ?? {message: ''}), open: true}; | ||
}); | ||
this.update(); | ||
} | ||
close(id: string) { | ||
this._$log.methodArgs?.('close', {id}); | ||
|
||
if (!this.snackBars[id]) return this._$log.warning('close', 'id_not_found', `'${id}' not found`); | ||
|
||
// onAfterClose() { | ||
// return new Promise<string>((resolve) => { | ||
// this.provider.subscribe(resolve, { | ||
// receivePrevious: false, | ||
// once: true, | ||
// priority: 1000, | ||
// }); | ||
// }); | ||
// } | ||
this.snackBars[id].functionalValue((old) => { | ||
return {...(old ?? {message: ''}), open: false}; | ||
}); | ||
this.update(); | ||
} | ||
|
||
protected update() { | ||
this._$log.methodArgs?.('update', {snackBars: this.snackBars}); | ||
this._$updaterContext.renotify(); | ||
} | ||
} |
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.