question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Theme] Provide a function to create the breakpoints property via DI

See original GitHub issue

Bug report

Right now, to use custom breakpoints, you can just pass extendTheme({ breakpoints: myBreakpointArray }) matching the spec.

However, if you manually pass, you’d be missing the properties defined on the default theme array.

We could provide a function or merge functionality for that into extendTheme, something like:

interface BreakpointConfig {
  [key: string]: string;
  sm: string;
  md: string;
  lg: string;
  xl: string;
}

const createBreakpoints = (config: BreakpointConfig) => {
 const breakpoints = Object.values(config);

 Object.entries(config).forEach(([key, value]) => {
  Object.defineProperty(breakpoints, key, { value });
 });

  return breakpoints;
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ljosberinncommented, Sep 4, 2020

A more complex, yet definitely more precise implementation would be based on this, credits to @0kku, but it requires TS 4.1, so we have to wait for next month for that.

type UnknownArray = ReadonlyArray<unknown>;

type Consumer<T> = (_: T) => void;

type UnionToOverloadedConsumer<Union> = UnionToIntersection<
  Union extends unknown
  ? Consumer<Union>
  : never
>;

type UnionLast<Union> = (
  UnionToOverloadedConsumer<Union> extends Consumer<infer Last>
  ? Last
  : never
);

type UnionExcludingLast<Union> = Exclude<
  Union,
  UnionLast<Union>
>;

type UnionToIntersection<Union> = (
  (Union extends unknown ? Consumer<Union> : never) extends Consumer<infer Intersection>
  ? Intersection
  : never
);

type TuplePrepend<
  Tuple extends UnknownArray,
  InsertedElement,
> = [InsertedElement, ...Tuple];

type UnionToTuple<
  Union,
  CurrentTuple extends UnknownArray = [],
> = (
  [Union] extends [never]
  ? CurrentTuple
  : UnionToTuple<
    UnionExcludingLast<Union>,
    TuplePrepend<
      CurrentTuple,
      UnionLast<Union>
    >
  >
);

interface BaseBreakpointConfig extends Record<string, string>{
  sm: string;
  md: string;
  lg: string;
  xl: string;
}

function createBreakpoints<T extends BaseBreakpointConfig>(config: T): Breakpoints<T> {
  return Object.assign(Object.values(config), config) as Breakpoints<T>;
}

type Breakpoints<T = BaseBreakpointConfig> = UnionToTuple<keyof T> & T;

const breakpoints = createBreakpoints({
  sm: "30em",
  md: "56em",
  lg: "91em",
  xl: "120",
} as const);

const a = breakpoints[0];
const b = breakpoints[1];
const c = breakpoints[2];
const d = breakpoints[3];
const e = breakpoints.sm;
const f = breakpoints.md;
const g = breakpoints.lg;
const h = breakpoints.xl;

It would support those goodies:

image image

1reaction
ljosberinncommented, Sep 4, 2020

possibly, that would lock in to forcing those object keys though; I’m not sure what’s planned in regards of allowing more/less/entirely custom breakpoint names

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Breakpoints and Media Queries in Material-UI
In order for us to use the theme and breakpoints, we will provide a function with the theme as a prop. In this...
Read more >
Using media queries - CSS: Cascading Style Sheets | MDN
Media queries allow you to apply CSS styles depending on a device's general type (such as print vs. screen) or other characteristics such...
Read more >
Breakpoint System Changes in Elementor v3.2.0
Custom breakpoints are used for generating custom CSS based on the values chosen by the user. Prior to v3.2.0, each breakpoint was registered...
Read more >
Breakpoints | IntelliJ IDEA Documentation - JetBrains
Breakpoints are special markers that suspend program execution at a specific point. This lets you examine the program state and behavior.
Read more >
Breakpoints - Material UI - MUI
CSS media queries are the idiomatic approach to make your UI responsive. The theme provides five styles helpers to do so: theme.breakpoints.up(key) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found