Last active
October 2, 2021 00:04
-
-
Save josemarluedke/a1500885db326606f8c257654cca6c04 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { getOwner } from '@ember/application'; | |
import type RouterService from '@ember/routing/router-service'; | |
function useService<T = unknown>(ctx: object, name: string): T { | |
return getOwner(ctx).lookup(`service:${name}`) as T; | |
} | |
function ref<T = unknown>(initialValue: T): Ref<T> { | |
return new Ref<T>(initialValue); | |
} | |
class Ref<T = unknown> { | |
@tracked value: T; | |
constructor(initialValue: T) { | |
this.value = initialValue; | |
} | |
} | |
function useRoutedDrawer( | |
ctx: object, | |
routeName: string | |
): { | |
isOpen: Ref<boolean>; | |
onClose: () => void; | |
didClose: () => void; | |
} { | |
const isOpen = ref<boolean>(true); | |
return { | |
isOpen, | |
onClose: (): void => { | |
isOpen.value = false; | |
}, | |
didClose: (): void => { | |
const router = useService<RouterService>(ctx, 'router'); | |
router.transitionTo(routeName); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: