如何扩展draft-js-emoji-plugin的主题

时间:2017-09-02 15:42:48

标签: javascript css reactjs draftjs draft-js-plugins

我只需在draft-js-emoji-plugin中扩展几个CSS规则。

记录方式是将theme对象传递给配置:

const theme = {
  emojiSelectButton: 'someclassname'
};
const emojiPlugin = createEmojiPlugin({theme});

不幸的是,这会覆盖整个主题类名而不是添加单个名。根据代码中的注释,此行为是设计的:

// Styles are overwritten instead of merged as merging causes a lot of confusion.
//
// Why? Because when merging a developer needs to know all of the underlying
// styles which needs a deep dive into the code. Merging also makes it prone to
// errors when upgrading as basically every styling change would become a major
// breaking change. 1px of an increased padding can break a whole layout.

In related issue开发人员建议导入draft-js-emoji-plugin/lib/plugin.css并在代码中扩展它。无论如何,这个文件中的每个类名都有后缀(CSS模块),它们可能会被更改,因此它是可靠的。

我不知道如何在不应对整个主题的情况下应用多种修复方法。

2 个答案:

答案 0 :(得分:1)

拥有这样的灵活性很好,但是重写所有类确实很痛苦。 我所做的是将所有类名称提取到一个对象,并使用styled-components将classNames插入到CSS定义中。这样,您可以扩展所需的任何内容,而不必担心样式化后缀的类(当他们改变版本时它会更改) 在此gist中,我刚刚复制了draft-js-emoji-plugin的v2.1.1版中的所有样式

const theme = {
  emoji: 'my-emoji',
  emojiSuggestions: 'my-emojiSuggestions',
  emojiSuggestionsEntry: 'my-emojiSuggestionsEntry',
  // ...
  emojiSelect: 'emojiSelect',
  emojiSelectButton: 'emojiSelectButton',
  emojiSelectButtonPressed: 'emojiSelectButtonPressed',
}

const StyledEmojiSelectWrapper = styled.div`
  .${theme.emojiSelect} {
      display: inline-block;
    }
  .${theme.emojiSelectButton}, .${theme.emojiSelectButtonPressed} {
    margin: 0;
    padding: 0;
    width: 2.5em;
    height: 1.5em;
    box-sizing: border-box;
    line-height: 1.2em;
    font-size: 1.5em;
    color: #888;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 1.5em;
    cursor: pointer;
  }
  .${theme.emojiSelectButton}:focus, .${theme.emojiSelectButtonPressed}:focus {
    outline: 0;
    /* reset for :focus */
  }
  // ...
`

export const GlobalStyleForEmojiSelect = createGlobalStyle`
  .${theme.emoji} {
    background-position: center;
    //...
  }

export default (props) => (
  <StyledEmojiSelectWrapper>
    <GlobalStyleForEmojiSelect />
    <EmojiSelect /> // lib button component
  </StyledEmojiSelectWrapper>
)

答案 1 :(得分:0)

更好的方法是从“ draft-js-emoji-plugin”导入{defaultTheme},然后将其扩展如下:

CloseableHttpClient

,因此可以根据需要使用插件。