Giter VIP home page Giter VIP logo

Comments (8)

olivierpascal avatar olivierpascal commented on August 30, 2024 1

My bad, when I meant 'I cannot externally theme the component anymore', i wanted to say that if a themeB is applied to a parent element of the component, since the component is already themed by themeA, themeA will always override themeB.

from stylex.

nmn avatar nmn commented on August 30, 2024

This is expected behaviour as it is how CSS variables work. To give you a peek behind the scenes, this the CSS that would be generated:

(I'm using readable names, but the actual output would use hashes)

:root {
  /* colorScheme = defineVars */
  --primary: red;
  /* myComponentVars = defineVars */
  --text: var(--primary);
}

.theme {
  --primary: green;
}

Setting the theme in JS, is essentially setting a class name which sets a new value for --primary. However, the value for --text has not changed. The value for --text was only set globally, and at that level --primary is still red.


Here's the workaround:

import React from 'react';
import { colorScheme } from './colorScheme.stylex';
import { myComponentVars } from './myComponentVars.stylex';

const theme = stylex.createTheme(colorScheme, {
  primary: 'green',
});
const componentTheme = stylex.createTheme(myComponentVars, {
  text: colorScheme.primary,
});

const styles = stylex.create({
  container: {
    color: colorScheme.primary, // <- theme is applied (output green)
    color: myComponentVars.text, // <- theme is not applied (output red)
  },
});

export const MyComponent = () => <div {...stylex.props(styles.container, theme, componentTheme)}>TEXT</div>;

This will re-assign the value of --text to the new local value of --primary.

from stylex.

olivierpascal avatar olivierpascal commented on August 30, 2024

@nmn The problem of this workaround is that I cannot externally theme the component anymore, because it's already using a theme internally.

from stylex.

nmn avatar nmn commented on August 30, 2024

Themes, created with CSS variables, don't need to be on the component itself. They can be set on a parent element.

from stylex.

olivierpascal avatar olivierpascal commented on August 30, 2024

Sure but it will be overridden by the local theme in the component div

from stylex.

nmn avatar nmn commented on August 30, 2024

@olivierpascal Since the component is not being styled by StyleX, I think you'll need to use a different system to override its styles.

from stylex.

nmn avatar nmn commented on August 30, 2024

Explain your problem a bit further. With some code examples?

from stylex.

nmn avatar nmn commented on August 30, 2024

Closing this issue as it seems addressed and there have been no follow-ups. Will re-open if you message again @olivierpascal

from stylex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.