【问题标题】:How to extend theme of draft-js-emoji-plugin如何扩展 Draft-js-emoji-plugin 的主题
【发布时间】:2017-09-02 15:42:47
【问题描述】:

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

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

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

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

// 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 模块),它们可能会被更改,所以它是可靠的。

我不知道如何在不处理整个主题的情况下应用多个修复程序。

【问题讨论】:

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


    【解决方案1】:

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

    import emojiPlugin, { defaultTheme } from 'draft-js-emoji-plugin';
    
    // say i need to extend the emojiSelectPopover's css then.
    
    defaultTheme.emojiSelectPopover = defaultTheme.emojiSelectPopover + " own-class"
    
    // own class is a class with your required enhanced css. this helps in preserving the old css.
    
    const emojiPlugin  = createEmojiPlugin({
        theme : defaultTheme
    })
    

    因此可以随意使用插件。

    【讨论】:

      【解决方案2】:

      拥有这样的灵活性很好,但重写所有类确实很痛苦。 我所做的是将所有类名提取到一个对象中,并使用styled-components 将类名插入到 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>
      )
      

      【讨论】:

        猜你喜欢
        • 2018-08-10
        • 2018-04-10
        • 2019-11-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-22
        • 2022-10-24
        • 2015-02-21
        • 1970-01-01
        相关资源
        最近更新 更多