Advertisement
Guest User

Untitled

a guest
Jun 28th, 2021
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const comeUpWithUniqueClassName = () =>
  2.   [...Array(5)]
  3.     .map(() => String.fromCharCode(Math.floor(Math.random() * 26 + 65)))
  4.     .join("");
  5.  
  6. function createAndInjectCSSClass(className, styles) {
  7.   const regex = / \\n/;
  8.   const style = styles[0].replace(regex);
  9.   const styleSheet = document.styleSheets[0];
  10.   styleSheet.insertRule(`
  11.     .${className} {
  12.       ${style}
  13.     }
  14.   `);
  15. }
  16.  
  17. const styled = (Tag) => (styles) => {
  18.   return function NewComponent(props) {
  19.     const uniqueClassName = comeUpWithUniqueClassName();
  20.     createAndInjectCSSClass(uniqueClassName, styles);
  21.     console.log(uniqueClassName);
  22.     return <Tag {...props} className={uniqueClassName} />;
  23.   };
  24. };
  25.  
  26. function Message({ children, ...delegated }) {
  27.   console.log(delegated);
  28.   // const styleSheet = document.styleSheets[0];
  29.  
  30.   return <p {...delegated}>You've received a message: {children}</p>;
  31. }
  32.  
  33. const UrgentMessage = styled(Message)`
  34.  background-color: pink;
  35.  color: white;
  36.  font-size: 1.5em;
  37.  font-weight: bold;
  38.  border: 5px solid red;
  39.  padding: 10px;
  40. `;
  41.  
  42. function App() {
  43.  return (
  44.    <div>
  45.      <UrgentMessage>
  46.        Lorem ipsum dolor sit amet consectetur adipisicing elit. Et, rerum?
  47.      </UrgentMessage>
  48.    </div>
  49.  );
  50. }
  51.  
  52. export default App;
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement