Skip to content

Commit 9e97ba6

Browse files
committed
animateViewTransition (with fallback)
1 parent 5a0229b commit 9e97ba6

3 files changed

Lines changed: 55 additions & 9 deletions

File tree

app/components/selection/delete.element.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DeleteStyles } from '../styles.store'
2+
import { animateViewTransition } from '../../utilities/'
23

34
export class Delete extends HTMLElement {
45
constructor() {
@@ -34,7 +35,7 @@ export class Delete extends HTMLElement {
3435
this.style.setProperty('--position', isFixed ? 'fixed' : 'absolute')
3536
}
3637

37-
deleteElement(e) {
38+
async deleteElement(e) {
3839
e.preventDefault()
3940
e.stopPropagation()
4041

@@ -44,6 +45,18 @@ export class Delete extends HTMLElement {
4445
}
4546

4647
this.observers.forEach(observer => observer.disconnect())
48+
49+
const elements = [
50+
this.targetElement,
51+
...this._linkedElementsToDeleteToo || [],
52+
...Array.from(document.querySelectorAll(`[data-label-id="${this.targetElement.getAttribute('data-label-id')}"]`)),
53+
this
54+
]
55+
56+
await animateViewTransition(elements, () => this.performDeletion())
57+
}
58+
59+
performDeletion() {
4760
this._linkedElementsToDeleteToo?.forEach(el => el.remove())
4861
this.targetElement.remove()
4962

app/features/selectable.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
metaKey, htmlStringToDom, createClassname, camelToDash,
1717
isOffBounds, getStyle, getStyles, deepElementFromPoint, getShadowValues,
1818
isSelectorValid, findNearestChildElement, findNearestParentElement,
19-
getTextShadowValues, isFixed, onRemove
19+
getTextShadowValues, isFixed, onRemove, animateViewTransition
2020
} from '../utilities/'
2121

2222
export function Selectable(visbug) {
@@ -159,8 +159,8 @@ export function Selectable(visbug) {
159159
e.preventDefault()
160160
}
161161

162-
const on_delete = e =>
163-
selected.length && delete_all()
162+
const on_delete = async e =>
163+
selected.length && await delete_all()
164164

165165
const on_clearstyles = e =>
166166
selected.forEach(el =>
@@ -497,29 +497,41 @@ export function Selectable(visbug) {
497497
!silent && tellWatchers()
498498
}
499499

500-
const delete_all = () => {
500+
const delete_all = async () => {
501501
const selected_after_delete = selected.map(el => {
502502
if (canMoveRight(el)) return canMoveRight(el)
503503
else if (canMoveLeft(el)) return canMoveLeft(el)
504504
else if (el.parentNode) return el.parentNode
505505
})
506506

507+
const elements = [
508+
...selected,
509+
...labels,
510+
...handles,
511+
rotationBtn,
512+
deleteBtn
513+
].filter(Boolean)
514+
515+
await animateViewTransition(elements, () => performDeletion())
516+
517+
selected_after_delete.forEach(el =>
518+
select(el))
519+
}
520+
521+
const performDeletion = () => {
507522
Array.from([
508523
...selected,
509524
...labels,
510525
...handles,
511526
rotationBtn,
512527
deleteBtn
513528
]).forEach(el => el.remove())
514-
529+
515530
labels = []
516531
handles = []
517532
selected = []
518533
rotationBtn = null
519534
deleteBtn = null
520-
521-
selected_after_delete.forEach(el =>
522-
select(el))
523535
}
524536

525537
const expandSelection = ({query, all = false}) => {

app/utilities/common.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,25 @@ export const onRemove = (element, callback) => {
131131
obs.observe(parent, {
132132
childList: true,
133133
})
134+
}
135+
136+
export async function animateViewTransition(elements, callback) {
137+
if (document.startViewTransition) {
138+
const transition = document.startViewTransition(() =>
139+
callback()
140+
)
141+
await transition.finished
142+
} else {
143+
const animations = elements.map(el =>
144+
el.animate([
145+
{ opacity: 1, transform: 'scale(1)' },
146+
{ opacity: 0, transform: 'scale(0.95)' }
147+
], {
148+
duration: 200,
149+
easing: 'ease-out'
150+
})
151+
)
152+
await Promise.all(animations.map(animation => animation.finished))
153+
callback()
154+
}
134155
}

0 commit comments

Comments
 (0)