matthewp / corset

Declarative data bindings, bring your own backend.
https://corset.dev/
BSD 2-Clause "Simplified" License
276 stars 2 forks source link

Media queries #24

Open matthewp opened 2 years ago

matthewp commented 2 years ago

Need a concrete use-case but it would be fun.

matthewp commented 2 years ago
@media only screen and (max-width: 768px) {
  .sidebar {
    mount: ${MobileSidebar};
  }
}

To dynamically load the mobile sidebar:

// This is a mountpoint which loads another mountpoint dynamically.
const mount = Symbol();
const Loadable = fn => state => {
  if(state[mount]) {
    return state[mount];
  }

  fn().then(mod => {
    // Triggers a reload.
    state[mount] = mod.default;
  });

  return sheet``;
};

mount(document, state => {
  return sheet`
    @media only screen and (max-width: 768px) {
      .sidebar {
        mount: ${Loadable(() => import('./mobile-sidebar.js'))};
      }
    }
  `;
});