4

我只需要在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.

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

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

4

2 回答 2

2

更好的方法是从“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
})

因此可以随意使用插件。

于 2020-06-11T04:42:22.980 回答
1

有这样的灵活性很好,但是重写所有的类真的很痛苦。我所做的是将所有类名提取到一个对象中,并使用 将类名styled-components插入到 css 定义中。这样你就可以扩展你想要的任何东西,而不必担心后缀类的样式(并且当它们碰撞版本时它会改变)在这个要点中,我刚刚复制了 v2.1.1 中的所有样式draft-js-emoji-plugin

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>
)
于 2019-04-11T13:38:41.113 回答