Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to React #35

Closed
yuanchuan opened this issue Sep 17, 2018 · 12 comments
Closed

Port to React #35

yuanchuan opened this issue Sep 17, 2018 · 12 comments

Comments

@yuanchuan
Copy link
Member

yuanchuan commented Sep 17, 2018

Current workaround:

It's not the best way, but if you want to use css-doodle in React you could try the following:

/* doodle.js */

import React from 'react';
import 'css-doodle';

export default ({ rule = '' }) => (
  <css-doodle>{ rule }</css-doodle>
)
/* example.js */

import Doodle from './doodle';

<Doodle rule={`
  :doodle {
    @grid: 2 / 200px;
    grid-gap: 1px;
  }
  background: @pick(red, pink);
`}/>

Better way:

Just use it directly!

import 'css-doodle';

function App() {
  return (
    <css-doodle>{`
      :doodle {
        @grid: 2 / 200px;
        grid: 1px;
      }
      background: @pick(red, pink);
    `}</css-doodle>
  );
}
@hiteshsahu
Copy link

Sample https://codesandbox.io/s/vjjq6686y0

@yuanchuan
Copy link
Member Author

@hiteshsahu Thank you :D

@simonmarton
Copy link

I think a template string helper would be nice too, similar to styled-components.
Something like this:

/* doodle.js */

import React from 'react';
import 'css-doodle';

export default ([ rule = '' ]) => () => (
  <css-doodle>{ rule }</css-doodle>
);
/* example.js */

import Doodle from './doodle';

const FancyDoodle = Doodle`
  :doodle {
    @grid: 2 / 200px;
    grid-gap: 1px;
  }
  background: @pick(red, pink);
`;

...

<FancyDoodle />

@yuanchuan
Copy link
Member Author

@simonmarton Nice!

@yuanchuan
Copy link
Member Author

yuanchuan commented May 27, 2019

No need to wrap around. Just use it directly!

import 'css-doodle';

function App() {
  return (
    <css-doodle>{`
      :doodle {
        @grid: 2 / 200px;
        grid: 1px;
      }
      background: @pick(red, pink);
    `}</css-doodle>
  );
}

@papan01
Copy link

papan01 commented Aug 21, 2019

Does this support tsx?

@yuanchuan
Copy link
Member Author

@papan01 Nope

@Mizumaki
Copy link

@papan01

Does this support tsx?

I don't know the support means, but I use <css-doodle> in Doodle.tsx, and it works.

First, you need to extend JSX.IntrinsicElements interface.

declare namespace JSX {
  interface IntrinsicElements {
    'css-doodle': {}
  }
}

And then, you can use <css-doodle>. (Unfortunately, it seems that there is no type definition yet.)

import React from 'react';
import 'css-doodle';

const Doodle: React.FunctionComponent = () => {
  // code is referenced from https://alligator.io/css/patterns-css-doodle/
  return (
    <css-doodle>
      {`
      :doodle {
        @grid: 5x8 / 100% 15rem;
      }
      :after {
        content: "@index()"
      }
      background: pink;
      margin: .5rem;
    `}
    </css-doodle>
  );
};

export default Doodle;

hmm.
Really nice 🥰

@papan01
Copy link

papan01 commented Aug 24, 2019

@Mizumaki That’s right! Thank you 🙇

@yuanchuan yuanchuan pinned this issue May 5, 2021
@yuanchuan yuanchuan unpinned this issue May 5, 2021
@Alfxjx
Copy link

Alfxjx commented Oct 18, 2021

import React from "react";

const Doodle = (rule) => () => {
	React.useEffect(() => {
		const script = document.createElement("script");
		script.src = "https://unpkg.com/css-doodle@0.20.2/css-doodle.min.js";
		document.body.appendChild(script);
	}, []);
	return <css-doodle>{rule}</css-doodle>;
};

export { Doodle };

use next.js and css-doodles

@vitpankin
Copy link

vitpankin commented Dec 26, 2021

For Typescript I had to add

declare global {
   declare namespace JSX {
      interface IntrinsicElements {
      'css-doodle': {}
    }
  }
}

@surajsharma
Copy link

anyone tried this with react-native?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants