Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

记录两个项目中遇到css相关的问题 #56

Open
shuangmianxiaoQ opened this issue Mar 24, 2020 · 0 comments
Open

记录两个项目中遇到css相关的问题 #56

shuangmianxiaoQ opened this issue Mar 24, 2020 · 0 comments
Labels
工作相关 记录工作中遇到的问题或收获

Comments

@shuangmianxiaoQ
Copy link
Owner

shuangmianxiaoQ commented Mar 24, 2020

参考:CSS Modules 详解及 React 中实践

Scss与JS变量共享时的报错

Css Module中可以使用:export关键字将Scss中的变量导出到JS中使用,但是在TS中引入时会提示找不到引入的scss模块,具体解决方式如下:

导出Scss变量

$textColor: #333;
$primaryColor: #0F0;
$secondaryColor: #F0F;

// Here's the export you add.
:export {
  textColor: $textColor;
  primaryColor: $primaryColor;
  secondaryColor: $secondaryColor;
}

为对应的scss文件书写d.ts声明文件,如下所示:

export interface I_globalScss {
  primaryColor: string;
  accentColor: string;
  textColor: string;
}

export const styles: I_globalScss;

export default styles;

外部样式覆盖局部样式方法

因为Css Module默认都是局部样式,带了hash串,当使用外部样式覆盖内部样式时不能正常修改,因为内外样式的hash不同,改的不是同一个。可以借助data-role属性的选择器来查找并修改覆盖样式:

// dialog.js
return <div className={styles.root} data-role='dialog-root'>
      <a className={styles.disabledConfirm} data-role='dialog-confirm-btn'>Confirm</a>
      ...
  </div>
// dialog.css
[data-role="dialog-root"] {
  // override style
}
@shuangmianxiaoQ shuangmianxiaoQ added the 工作相关 记录工作中遇到的问题或收获 label May 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
工作相关 记录工作中遇到的问题或收获
Projects
None yet
Development

No branches or pull requests

1 participant