Z
Z
Zhanna_K2020-10-20 18:29:39
Sass
Zhanna_K, 2020-10-20 18:29:39

Can props-derived selectors be used with Sass modules in React?

I want to encapsulate button styles. How can I pass selectors obtained from props to className instead of selectorOne, selectorTwo if I use modules?

Or how do normal people act in this case?

import React from "react";
import classNames from "classnames";
import styles from "./_submit.module.scss";

const SubmitButton = ({ handleClick, text, ...selectors }) => {
  return (
     <button className={classNames(styles.selectorOne, styles.selectorTwo)} onClick={handleClick}>
      {text}
    </button>
  );
};

export default SubmitButton;


_submit.module.scss
@import "utils/_config.scss";

.btn {
  @include h3;
  cursor: pointer;
  border-radius: 22px;
  height: 44px;
}

.outlined {
  color: $purple;
  border: 1px solid $purple;
}
.contained {
  color: white;
  background: $gradient;
  border: none;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zhanna_K, 2020-10-20
@Zhanna_K

Decided:

import React from "react";
import classNames from "classnames";
import styles from "./_submit.module.scss";
const SubmitButton = ({ handleClick, text, selector }) => {
  let isOutlined;
  selector === "contained" ? (isOutlined = false) : (isOutlined = true);
  return (
    <button
      className={classNames(
        { [styles.contained]: !isOutlined },
        { [styles.outlined]: isOutlined },
        { [styles.btn]: true }
      )}
      onClick={handleClick}
    >
      {text}
    </button>
  );
};

export default SubmitButton;

That's how it all works!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question