Expose a inlineScript function #218
Replies: 3 comments
-
|
Beta Was this translation helpful? Give feedback.
-
So we already have a inlined script through <div onClick$={}>
<div window:onClick$={}>
<div navigate:onNavigate$={}>
<div media:on????$={}> It is unclear how <div media:media-tablet="(min-width:600px)" window:media-tablet={() => console.log('click!!')}/>
|
Beta Was this translation helpful? Give feedback.
-
@mhevery thanks for the feedback. I'm not sure if qwik should provide an API for every event of every browser EventTarget, I think it's going to be a never ending task. The main issue with sync$ is that it doesn't accept variables. So we cannot pass the mediaQuery string for example. The unexposed _inlinedQrl allow that with some string manipulation. But it's not exposed and it's very low level, not super dev friendly. Maybe qwik could update the // Bind "this" if there is an argument
export function sync$<T extends Object>(fn: (this: T, ...args: any[]) => void, arg?: T) {
if (!arg) return _qrlSync(fn.bind(arg), fn.toString()) as any as QRL<EventHandler>
const fnStr = `(${fn.toString()}).bind(${JSON.stringify(arg)})`;
return _qrlSync(fn.bind(arg), fnStr) as any as QRL<EventHandler>
}
const useMedia = (mediaQuery: string, qrl: QRL<(e: CustomEvent) => any>) => {
const customEvent = useId();
useOnDocument(customEvent, qrl);
useOnDocument('qvisible', sync$(function (e, el) {
const handler = (ev) => document.dispatchEvent(new CustomEvent(this.customEvent, {detail:ev}));
const media = matchMedia(this.mediaQuery);
media.addEventListener('change', handler);
// this event doesn't exist yet. See: https://github.com/QwikDev/qwik-evolution/issues/219
el.addEventListener('qCleanup', () => media.removeEventListener('change', handler), { once: true });
},
// Params will be bound to this
{
customEvent,
mediaQuery
}));
} |
Beta Was this translation helpful? Give feedback.
-
What is it about?
Being able to add an inlined script from a hook
What's the motivation for this proposal?
Problems you are trying to solve:
I'm trying to build event listener around Browser API which are not
window
,document
orElement
. For example :matchMedia('(min-width: 600px)').addEventListener('change', cb)
->useMedia('(min-width: 600px)', $(cb))
navigation.addEventListener('navigate')
-> `useNavigation('navigate', $(cb))Currently this only way to achieve that is with
useVisibleTask$
oruseOnDocument('DOMLoaded')
which will both download core early.Goals you are trying to achieve:
Start listening on event from the browser without having to download core until the event is triggered.
Any other context or information you want to share:
Some API require custom values (like
matchMedia
which needs the media query) so we cannot build a generic hook withsync$
Proposed Solution / Feature
What do you propose?
Export
_qrlSync
function publicly to be able to append an inline script from a hook.We can name it
inlineScript
for example.Code examples
It could look like that for example
Links / References
No response
Beta Was this translation helpful? Give feedback.
All reactions