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.

Multiple inheritance (Composition)

See original GitHub issue

Is or would the following be possible?

export const Card = styled(Rounded, Column)

otherwise I have to do the following

export const Card = styled(RoundedColumn)

Which is alright, but add more code.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

149reactions
kittencommented, May 6, 2017

I’d suggest you to use mixins with the css helper in this case, since it’s not really an inheritance problem, but a compositional problem.

Let’s say you put the code for rounded elements in a rounded mixin:

import { css } from 'styled-components'

const rounded = css`
  border-radius: 5px;
`

And the one for columns in a column mixin:

import { css } from 'styled-components'

const column = css`
  display: block;
  flex-shrink: 0;
  flex-grow: 0;
  flex-basis: 25%;
  width: 25%;
`

Then you’d define your card like this:

import styled from 'styled-components'

const Card = styled.div`
  ${rounded}
  ${column}
`

We’re still missing a section on mixins / composition in the new documentation, but an explanation of the helper itself can be found here: https://sc-next-docs.philpl.com/docs/api#css

Edit: I changed the title in the hopes that someone searching for this, will have an easy time finding it 😃

4reactions
kittencommented, Aug 8, 2018

@joetidee this would be a question that’d be more suited for spectrum or a new issue, but let me elaborate on it here 😃

First of all I’ll change the two categories of answers up a little bit. As a CSS-in-JS library they don’t really suit either disciplines we span perfectly.

The mixins in styled-components or similar libraries are rather different than pure compile-to-CSS mixins (think Sass), since they exist during JS runtime.

So A and B would sound more like the following to me:

A) Is the mixin computed into a combined CSS template in styledComponents, or

B) Are separate CSS classes created for mixins?

In styled-components the answer is: A. Mixins are flattened into a StyledComponent’s style “template.”

This doesn’t have any effect on your everyday-use. Even in SSR this is negligible as long as your SSR results eventually are gzip’d.

B is not an option in the answers you’ve outlined since they’re just tagged templates, so arrays of strings and interpolations.

They’re still functionally composed in that, without side effects they’re merged into the rest of a StyledComponent’s template.

But they’re also technically preevaluated at runtime, which means they’re “copied” into another template.

This is to preserve specificity, which trumps everything else in our case. We expect the mixin to always apply at the case you interpolate it into, respectless of what comes after. If something after the mixin overrides its rules that needs to be respected. Hence we merge the mixin into a component’s rules. Which is also important when a mixin is used multiple times.

At runtime we make sure that the tagged template literal css function already preformats the rules to evaluate non-function interpolations, so this is as efficient as we can make it.

Tl;dr: in SC mixins are inlined into the rest of your rules during runtime. This leads to duplication only during SSR, which is fine with gzip.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How is composition different than multiple inheritance?
It depends what you mean by "multiple inheritance" and "composition." If composition and inheritance both just mean adding to a list of ...
Read more >
Composition over inheritance
Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should achieve polymorphic ...
Read more >
Inheritance and Composition: A Python OOP Guide
Composition is more flexible than inheritance because it models a loosely coupled relationship. Changes to a component class have minimal or no effects...
Read more >
Multiple Inheritance in Java
Composition helps us in providing controlled access to the superclass methods whereas inheritance doesn't provide any control of the superclass ...
Read more >
Java inheritance vs. composition: How to choose
Inheritance and composition are two programming techniques developers use to establish relationships between classes and objects.
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