Skip to content
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

Add rememberBreakpoint to Silk #118

Closed
bitspittle opened this issue Feb 4, 2022 · 5 comments
Closed

Add rememberBreakpoint to Silk #118

bitspittle opened this issue Feb 4, 2022 · 5 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@bitspittle
Copy link
Collaborator

This could be used to toggle code with logic, as an additional path besides component styling.

val breakpoint = rememberBreakpoint()
if (breakpoint >= Breakpoint.MD) {
   ... show this only on desktop ...
}
@bitspittle bitspittle added the enhancement New feature or request label Feb 4, 2022
@bitspittle bitspittle added this to the 1.0 milestone Feb 4, 2022
@bitspittle bitspittle self-assigned this Feb 4, 2022
@bitspittle
Copy link
Collaborator Author

So this could be tricky - it doesn't seem like there's a good way to compare sizes of different units.

In other words, I can easily get the "px" value of the current window, but I have no idea if users defined their breakpoints as "px", "em", "rem", or some other relevant user size.

It could be easier to force breakpoints to always be "px" values but looking at this for example: https://chakra-ui.com/docs/features/responsive-styles it seems like using font-size related sizes may not be uncommon?

@bitspittle
Copy link
Collaborator Author

Here's an interesting article on the topic: https://medium.com/zoosk-engineering/to-em-or-not-to-em-that-is-the-media-query-question-22f4a65e9747

Feels like breakpoints should either be px OR em. Maybe I can force breakpoints to be one or the other behind the scenes, or a sealed class to limit to only those two types, which may allow this feature to work (assuming I can at least convert between em and px in javascript)

@bitspittle
Copy link
Collaborator Author

@bitspittle
Copy link
Collaborator Author

Maybe this, which lets you convert from px to em in code:

https://stackoverflow.com/a/12241338/17966710

@bitspittle
Copy link
Collaborator Author

OK, got this to work...

  • Restricted breakpoint values to "px", "em", and "rem" units (for now)
  • Ensured we can convert all breakpoint units to "px"
  • Used "window.getComputedStyle(document.body).getValueProperty("font-size") for "em" and "rem" values
    • Hopefully this is not fragile, I guess we'll see
  • Added a windows resize event listener which takes advantage of these new constraints to produce breakpoint state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Kobweb Frontend
Awaiting triage
Development

No branches or pull requests

1 participant