linaria icon indicating copy to clipboard operation
linaria copied to clipboard

Support defining all class selector of the component at once?

Open tofrankie opened this issue 2 years ago • 1 comments

The following discussion focuses on business scenarios, rather than writing component library.

Describe the feature

Does linaria support defining all class selector for a component at once?

Similar to the stylesheet method of astroturf:

import { stylesheet } from 'astroturf'

const styles = stylesheet`
  .box {
    width: 100%;
    height: 50px;
  }

  .btn {
    width: 80%;
    heigth: 100%;
    margin: 0 auto;
    border: 1px solid #4096ff;
  }
`

const Button = () => (
  <div className={styles.box}>
    <button className={styles.btn}>Click me</button>
  </div>
)

linaria has similar methods like:

import { css } from '@linaria/core'

const styles = {
  box: css`
    width: 100%;
    height: 50px;
  `,
  btn: css`
    width: 80%;
    heigth: 100%;
    margin: 0 auto;
    border: 1px solid #4096ff;
  `,
}

const Button = () => (
  <div className={styles.box}>
    <button className={styles.btn}>Click me</button>
  </div>
)

If a component has many styles, you need to add a lot of prop: css`xxx`, which is cumbersome and not elegant.

Motivation

As above. In business scenarios, it is very common for a component to have multiple class selector.

Possible implementations

...

Related Issues

tofrankie avatar Jan 30 '23 11:01 tofrankie

I can see how your proposal would require less switching between CSS and JS syntax when writing styles but how would you make this type-safe? Will typescript actually complain if styles.box was misspelled? Would a simple rename to styles.container keep the stylesheet in sync? I haven't worked with astroturf yet but I doubt this would work. And if type-safety is not a concern, css-modules are surely the better choice for this style of writing css.

espretto avatar Mar 26 '25 11:03 espretto