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

@morfeo/css | accept functions in createUseClasses function #647

Closed
mauroerta opened this issue Jan 29, 2023 · 1 comment · Fixed by #692
Closed

@morfeo/css | accept functions in createUseClasses function #647

mauroerta opened this issue Jan 29, 2023 · 1 comment · Fixed by #692
Assignees

Comments

@mauroerta
Copy link
Collaborator

mauroerta commented Jan 29, 2023

The idea is to change the interface of createUseClasses into:

import { Style } from "@morfeo/web";


type ClassStyle = Style | {
  [K in keyof Style]: (props: Record<string, unknown>) => Style[K]
}

export function createUseClasses<Keys extends string>(
  styles: Record<Keys, ClassStyle>,
): () => Record<Keys, string> {
  // ...
}

This way it will be possible to use it in this way:

const useClasses = createUseClasses({
   button: {
     p: 'm',
     bg: (props) => props.background
   }
})

const Button = ({ background }) => {
  const classes = useClasses({ background });
  return <button className={classes.button} />
}

In this case, the resulting code will (probably) become:

const getClasses = (props) => ({
   button: `p-m bg-${getClassName((props) => props.background)(props)}`)
})

const Button = ({ variant }) => {
  const classes = getClasses({ background });
  return <button className={classes.button} />
}

And the generated CSS has to provide all the possible styles instead of the specific one:

.p-m {
   padding: 1.5rem;
}

.bg-primary {
   /* the generated css */
}

/* all the possible variants of the button */

.bg-success {
   /* the generated css */
}

.bg-error {
   /* the generated css */
}
@mauroerta mauroerta converted this from a draft issue Jan 29, 2023
@mauroerta mauroerta self-assigned this Jan 29, 2023
@mauroerta
Copy link
Collaborator Author

mauroerta commented Jan 29, 2023

Any other proposal is, of course, welcome

@mauroerta mauroerta moved this from Todo to In Progress in morfeo v1 Jan 31, 2023
mauroerta pushed a commit that referenced this issue Apr 12, 2023
mauroerta pushed a commit that referenced this issue Apr 12, 2023
mauroerta pushed a commit that referenced this issue Apr 13, 2023
mauroerta pushed a commit that referenced this issue Apr 13, 2023
mauroerta added a commit that referenced this issue Apr 14, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in morfeo v1 Apr 14, 2023
@mauroerta mauroerta moved this from Done to Released in morfeo v1 Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Released
Development

Successfully merging a pull request may close this issue.

1 participant