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.

Improve TypeScript support for media templates

See original GitHub issue

Typing media templates can be cumbersome. I’ve spent a fair amount of time to figure out the following type declarations and casts compliant with TypeScript’s strict mode:

import { ThemedCssFunction } from 'styled-components';
import { css } from './styled-components';
import ThemeInterface from './theme';

const sizes = {
  small: 600,
  medium: 1024,
  large: 1440,
  xlarge: 1920,
};

// Iterate through the sizes and create a media template
export const media = (Object.keys(sizes) as (keyof typeof sizes)[]).reduce(
  (acc, label) => {
    acc[label] = (first: any, ...interpolations: any[]) => css`
      @media (min-width: ${sizes[label]}px) {
        ${css(first, ...interpolations)}
      }
    `;

    return acc;
  },
  {} as { [key in keyof typeof sizes]: ThemedCssFunction<ThemeInterface> },
);

One of the possible solutions is writing docs including the aforementioned code snippet. An other way to get around the issue and lower the amount of boilerplate code required even for vanilla JS projects is to create a method which encapsulates the functionality above:

import { mediaTemplates } from 'styled-components';

const media = mediaTemplates({
  small: 600,
  medium: 1024,
  large: 1440,
  xlarge: 1920,
});

Issue Analytics

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

github_iconTop GitHub Comments

71reactions
kachkaevcommented, Apr 4, 2019

Many thanks for your suggestion @fabb! I refactored my media templates as you suggest and all typescript issues were gone, plus I got better autoformatting by Prettier in a couple of places 🎉

Wondering if docs could be changed to this:

const customMediaQuery = (maxWidth: number) =>
  `@media (max-width: ${maxWidth}px)`;

const media = {
  custom: customMediaQuery,
  desktop: customMediaQuery(922),
  tablet: customMediaQuery(768),
  phone: customMediaQuery(576)
};

const Content = styled.div`
  height: 3em;
  width: 3em;
  background: papayawhip;

  /* Now we have our methods on media and can use them instead of raw queries */
  ${media.desktop} {
    background: dodgerblue;
  }
  ${media.tablet} {
    background: mediumseagreen;
  }
  ${media.phone} {
    background: palevioletred;
  }
`;

render(<Content />);
  • Looks much simpler and easier to read
  • 100% understood by Prettier
  • Perfectly works in TS with no added magic
  • Is probably a bit faster because additional calls to css`...` are removed 🎉

@probablyup do you think this would make sense? I can submit a PR if that’s potentially interesting.

3reactions
fabbcommented, Mar 19, 2019

Imho writing media templates as template functions is not a good idea, because linting, syntax highlighting and auto completion won‘t work with its content css.

It is way eqsier and imho better to just create strings with the @media definitions:

const phone = „@media (max-width: 480px)“

const MyComponent = styled.div`
    background: palevioletred;
    ${phone} {
        background: dodgerblue;
    }
`

The media definitions could also read from the theme, this would work similarly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modernizing Our Image Template Service with React ...
Modernizing Our Image Template Service with React, TypeScript, Chrome, and Kubernetes - How and Why We Re-wrote Our Busiest Service.
Read more >
Improving the TypeScript support in Azure Functions
Included with these changes are a set of templates for TypeScript, type definitions, and npm scripts. Read on to learn more details about...
Read more >
TypeScript support, Media library folders available in Strapi v4.3
Cover image for TypeScript support, Media library folders available ... Write your Strapi application code or develop plugins in TypeScript ...
Read more >
Introducing TypeScript support for building AWS ...
In this blog post, we showed you how to develop, test, and register a resource type authored in TypeScript. The CloudFormation CLI and...
Read more >
Documentation - TypeScript 3.9
While there's still room for improvement, we hope this work translates to a ... Visual Studio Code supports selecting different versions of TypeScript....
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