项目作者: rgdelato

项目描述 :
styled-components without the components
高级语言: JavaScript
项目地址: git://github.com/rgdelato/styled-classnames.git
创建时间: 2017-01-21T00:12:46Z
项目社区:https://github.com/rgdelato/styled-classnames

开源协议:MIT License

下载


Warning: This library is no longer being maintained!

I would strongly recommend using another CSS-in-JS library, such as Emotion

styled-classnames

Use styled-components without the components!

…it’s a short hack that still uses styled-components under the hood, so all the credit for this goes to Glen Maddern, Max Stoiber, and the styled-components community.

Be aware that including styled-classnames adds a noticeable amount to your final JS bundle size.

Javascript

npm version

  1. npm install --save-dev styled-classnames
  1. import React from "react";
  2. import Link from "react-router/Link";
  3. import styled from "styled-classnames";
  4. const linkClassName = styled`
  5. color: ${props => props.color};
  6. margin: 3px;
  7. padding: 3px 7px;
  8. text-decoration: none;
  9. border: 1px solid transparent;
  10. border-radius: 3px;
  11. &:hover { border-color: rgba(175, 47, 47, 0.1); }
  12. `;
  13. const activeClassName = styled`
  14. border-color: rgba(175, 47, 47, 0.2);
  15. `;
  16. const FilterLink = (props) => {
  17. return (
  18. <Link
  19. {...props}
  20. className={linkClassName(props)}
  21. activeClassName={activeClassName(props)}
  22. />
  23. );
  24. };
  25. export default FilterLink;

ClojureScript

Clojars Project

  1. (ns demo.core
  2. (:require [reagent.core :as reagent]
  3. cljsjs.styled-classnames))
  4. (def font-family (atom "Baskerville"))
  5. (def home-class (js/styled "
  6. font-size: 72px;
  7. font-family: " @font-family ";
  8. font-style: italic;
  9. color: " #(:color %) ";
  10. "))
  11. (defn home-page []
  12. [:div
  13. [:h2 {:class (home-class {:color "rebeccapurple"})}
  14. "Welcome to Reagent"]])

Details

Helper methods from styled-components are also exposed:

  1. import styled, { css, keyframes, injectGlobal } from "styled-classnames";

Note that ThemeProvider and withTheme are React-specific and are not included in this library. If you need them, you can still import them from styled-components, or if you know how context works in React, you could write them yourself.