Expose a inlineScript function #218
Replies: 5 comments 3 replies
-
|
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.
-
Correct. But it can't pass the variables without the framework deserialization being present. You suggest string concatenation, which feels very dirty, and I am not sure how that would work with SSR. The workaround is for you to put your variables into DOM attributes and than the
Yes not sure we should allow such low level hacks. My current inclination is to close this request as wont do since the |
Beta Was this translation helpful? Give feedback.
-
|
@GrandSchtroumpf You might be interested in q:args which would add extra arguments to event handlers #312 (but of course only once the framework woke up) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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,documentorElement. 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
matchMediawhich needs the media query) so we cannot build a generic hook withsync$Proposed Solution / Feature
What do you propose?
Export
_qrlSyncfunction publicly to be able to append an inline script from a hook.We can name it
inlineScriptfor example.Code examples
It could look like that for example
Links / References
No response
Beta Was this translation helpful? Give feedback.
All reactions