Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Add ClassNames support to the customStyleMap #2164

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

newsiberian
Copy link

@newsiberian newsiberian commented Aug 21, 2019

Summary

This PR makes it possible to pass classNames (strings) instead of styles (objects) to the customStyleMap

Resolves #957, resolves #1368, resolves #1639
and partly related with #342, #521

If you will accept this PR, please, let me know which part of documentation should be updated and how

Test Plan

This changes can be tested via Color example which was slightly modified

// minification by themselves. This is handy when used libraries like `jss`
// or similar
{className: classNames.join(' ').trim()}
: {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting classes to null or undefined would already create a dom node without an empty class attribute.

https://codesandbox.io/s/upbeat-yalow-uwkoh

Spreading here is just unnecessary and not good for perf.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for looking into this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

@voidpumpkin
Copy link

@newsiberian Do you plan to revisit this? I would love to have this feature.

@newsiberian
Copy link
Author

What for? I did what @fabiomcosta asked. We are successfully use this for about the year

@voidpumpkin
Copy link

voidpumpkin commented Apr 22, 2020

@fabiomcosta Oh my bad, I somehow skipped that. But thx for the response.

@voidpumpkin
Copy link

@newsiberian Sorry, even with that information i cannot figure out how to add a custom css class to a existing style like "BOLD".

Here is my customStyleMap :

const styleMap = {
    BOLD: 'myClass',
};

Could you help me out?

@newsiberian
Copy link
Author

newsiberian commented Apr 22, 2020

we use something like this:

export const getStyleMap = (theme, classes) => ({
  [FONT_COLOR_ONE]: classes.fontColorOne,
  [FONT_COLOR_TWO]: classes.fontColorTwo,
  [FONT_COLOR_THREE]: classes.fontColorThree,
  [FONT_COLOR_FOUR]: classes.fontColorFour,
  [FONT_COLOR_FIVE]: classes.fontColorFive,
  [FONT_SIZE_14]: classes.fontSize14,
  [FONT_SIZE_16]: classes.fontSize16,
  [FONT_SIZE_18]: classes.fontSize18,
  [FONT_SIZE_20]: classes.fontSize20,
  [FONT_SIZE_24]: classes.fontSize24,
  [FONT_SIZE_28]: classes.fontSize28,
  [FONT_SIZE_32]: classes.fontSize32,
  [FONT_SIZE_36]: classes.fontSize36,
  [FONT_SIZE_42]: classes.fontSize42,
  [FONT_SIZE_48]: classes.fontSize48,
  [FONT_SIZE_54]: classes.fontSize54,
  [FONT_SIZE_60]: classes.fontSize60,
  [VERTICAL_ALIGN_TOP]: {
    verticalAlign: 'super',
  },
  [VERTICAL_ALIGN_BOTTOM]: {
    verticalAlign: 'sub',
  },
});

then, in component:

// theme / classes from material-ui solution
const memoizedStyleMap = useMemo(() => getStyleMap(theme, classes), [
    theme,
    classes,
  ]);

// then
return <Editor ...props styleMap={memoizedStyleMap} />

so, it is similar like you do

@voidpumpkin
Copy link

voidpumpkin commented Apr 22, 2020

@newsiberian So wait, where are those classes, are they css in js or actual css?
EDIT: I ended up digging around in draft-js implementation and you have to be using inline styles. Pure css won't work.
My solution was to do this selector:

        editorContainer span[style*='font-weight: bold;'] {
            ...
        }

@DaniGuardiola
Copy link

Is this getting merged? @fabiomcosta

Thanks :)

@fabiomcosta
Copy link
Contributor

@DaniGuardiola I'm actually not a maintainer of this project, but contribute here and there.
You might want to ask @claudiopro or @mrkev

let styleObj = styleSet.reduce((map, styleName) => {
const mergedStyles = {};
const style = customStyleMap[styleName];
let style;
if (typeof customStyleMap[styleName] === 'string') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe would be better to add a new prop customCssClassMap. This way you don't need this check.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this approach here #2930

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your version looks cleaner. I like it)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants