Fixed expanded sections not collapsing on first click

This commit is contained in:
squidfunk
2021-03-04 18:18:56 +01:00
parent f87e32debe
commit f05c34e30d
6 changed files with 68 additions and 50 deletions

View File

@@ -20,7 +20,14 @@
* IN THE SOFTWARE.
*/
import { Observable } from "rxjs"
import { Observable, fromEvent, of } from "rxjs"
import {
mapTo,
mergeMap,
switchMap,
takeWhile,
tap
} from "rxjs/operators"
import { getElements } from "~/browser"
@@ -50,13 +57,24 @@ interface PatchOptions {
export function patchIndeterminate(
{ document$ }: PatchOptions
): void {
document$.subscribe(() => {
for (const el of getElements<HTMLInputElement>(
"[data-md-state=indeterminate]"
)) {
el.setAttribute("data-md-state", "")
el.indeterminate = true
el.checked = false
}
})
document$
.pipe(
switchMap(() => of(...getElements<HTMLInputElement>(
"[data-md-state=indeterminate]"
))),
tap(el => {
el.indeterminate = true
el.checked = false
}),
mergeMap(el => fromEvent(el, "change")
.pipe(
takeWhile(() => el.hasAttribute("data-md-state")),
mapTo(el)
)
)
)
.subscribe(el => {
el.removeAttribute("data-md-state")
el.checked = false
})
}