Skip to content

Mediaquery filter #9216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

BurningTreeC
Copy link
Contributor

This PR adds a mediaquery filter operator to TiddlyWiki5

Example use:

{{{ [[$:/palette]mediaquery[(prefers-color-scheme: light)]get[text]get[color-scheme]] [[$:/palettedark]mediaquery[(prefers-color-scheme: dark)]get[text]get[color-scheme]] }}}

In this PR it's used to alter the <<colour>> macro and to determine the color-scheme on the :root element like above

add mediaquery filter operator
use mediaquery filter operator to determine which palette to use
use mediaquery filter operator in base vanilla stylesheet to determine the color-scheme
Copy link

netlify bot commented Aug 3, 2025

Deploy Preview for tiddlywiki-previews ready!

Name Link
🔨 Latest commit cc6a010
🔍 Latest deploy log https://app.netlify.com/projects/tiddlywiki-previews/deploys/688fb869fb34760008c6f48c
😎 Deploy Preview https://deploy-preview-9216--tiddlywiki-previews.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

github-actions bot commented Aug 3, 2025

Confirmed: BurningTreeC has already signed the Contributor License Agreement (see contributing.md)

Copy link

github-actions bot commented Aug 3, 2025

📊 Build Size Comparison: empty.html

Branch Size
Base (master) 2537.6 KB
PR 2541.2 KB

Diff: ⬆️ Increase: +3.6 KB

@@ -4,7 +4,7 @@ tags: $:/tags/Macro
<!-- Needs to stay that way for backwards compatibility. See GH issue: #8326 -->
\define colour(name)
\whitespace trim
<$transclude tiddler={{$:/palette}} index="$name$">
<$transclude tiddler={{{ [[$:/palettedark]mediaquery[(prefers-color-scheme: dark)]get[text]] :else[[$:/palette]get[text]] }}} index="$name$">
Copy link
Member

@pmario pmario Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the braces used for? (prefers-color-scheme: dark)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just how a media query looks like. Should they be removed? 😊

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did read the MDN docs and saw the Parameters docs paragraph. It says the braces are needed ...

But in TW filter syntax, they may cause confusion. So I am not sure anymore.

For the time being, I think we should keep it as it is atm. In the docs we will need to make sure, that the braces belong to the query and not to the filter syntax. ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the braces but then we need to add them programmatically

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MDN says every media-feature must be wrapped into parentheses. So it could be added by the filter -- But I am not really sure about that one. -- What if users search the web and see, that parentheses are needed, so they add them. We will end up with 2 of them. IMO for the time being it should be as it is. (Just my gut feeling)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok yes I think you're right that leaving the () is the best

@pmario
Copy link
Member

pmario commented Aug 3, 2025

Do you know the draft PR: #8702 from Jeremy?


// Only evaluate in browser environment
if($tw.browser && mediaQuery && widget) {
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is the if() statement, which should catch all problems. We need to make sure, that the variables are initialised. So why do we still need a try/catch statement here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see. window.matchMedia() does not throw any exceptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove the try catch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, the try catch is indeed needed. Invalid media queries throw exceptions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure the window.matchMedia() is throwing or is it something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test, I'm not quite sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right @pmario , it doesn't throw exceptions. I've removed the try/catch statement

@BurningTreeC
Copy link
Contributor Author

Do you know the draft PR: #8702 from Jeremy?

Yes I vaguely remember. But this is more about the filter operator than about palettes. I just added them as an example


// Add the listener (use modern addEventListener if available)
if(mql.addEventListener) {
mql.addEventListener("change", changeHandler);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw this. I am not sure, if you can add an event-handler to a filter operator
@Jermolene - Is that possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible in terms of it's doable 😁 but if it's allowed in the core - that's a different question.
But this event listener would make this filter operator special because it doesn't react to tiddler changes but to media-query changes

As @pmario mentioned and after some tests, this should not throw errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants