我需要在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模块),它们可能会被更改,因此它是可靠的。
我不知道如何在不复制整个主题的情况下应用多个文件。
拥有这样的灵活性很好,但重写所有类真的很痛苦。我所做的是将所有类名提取到一个对象,并使用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>
)