Skip to content

Commit

Permalink
✨ feat(react): 添加样式
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Mar 25, 2022
1 parent 39d6ff0 commit e7b7ea2
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
8 changes: 6 additions & 2 deletions docs/components/example.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { AccountBookFilled } from '@lotus-design/icons';
import React from 'react';
import { AccountBookFilled, AccountBookOutlined, } from '@lotus-design/icons';

export default () => {
return (
<AccountBookFilled />
<>
<AccountBookFilled />
<AccountBookOutlined spin />
</>
)
}
3 changes: 2 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"dependencies": {
"@ant-design/colors": "^6.0.0",
"@lotus-design/icons-svg": "^1.0.0",
"@pansy/classnames": "^1.0.1"
"@pansy/classnames": "^1.0.1",
"rc-util": "^5.19.3"
},
"author": "",
"license": "MIT"
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/components/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import classNames from '@pansy/classnames';
import Context from './Context';
import { svgBaseProps } from '../utils';

import { useInsertStyles } from '../hooks/useInsertStyles';

export interface IconBaseProps extends React.HTMLProps<HTMLSpanElement> {
spin?: boolean;
rotate?: number;
Expand Down Expand Up @@ -42,6 +44,8 @@ const Icon = React.forwardRef<HTMLSpanElement, IconComponentProps>(({
...restProps
}, ref) => {

useInsertStyles();

const { prefixCls = 'lotus-icon' } = React.useContext(Context);

const classString = classNames(
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/components/IconBase.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { AbstractNode, IconDefinition } from '@lotus-design/icons-svg/lib/types';

import { useInsertStyles } from '../hooks/useInsertStyles';
import { getSecondaryColor, isIconDefinition, generate, } from '../utils';

export interface IconProps extends Partial<TwoToneColorPaletteSetter> {
Expand Down Expand Up @@ -56,6 +57,8 @@ const IconBase: IconBaseComponent<IconProps> = ({
secondaryColor,
...restProps
}) => {
useInsertStyles();

let colors: TwoToneColorPalette = twoToneColorPalette;
if (primaryColor) {
colors = {
Expand Down
69 changes: 69 additions & 0 deletions packages/react/src/hooks/useInsertStyles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useContext, useEffect } from 'react';
import IconContext from '../components/Context';
import { updateCSS } from 'rc-util/lib/Dom/dynamicCSS';


export const useInsertStyles = (styleStr?: string) => {
const { csp, prefixCls = 'lotus-icon' } = useContext(IconContext);

useEffect(() => {
updateCSS(styleStr ?? getIconStyles(prefixCls), 'lotus-design-icons', {
prepend: true,
csp,
});
}, []);
};

function getIconStyles (prefixCls: string) {
return `
.${prefixCls} {
display: inline-block;
color: inherit;
font-style: normal;
line-height: 0;
text-align: center;
text-transform: none;
vertical-align: -0.125em;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.${prefixCls} > * {
line-height: 1;
}
.${prefixCls} svg {
display: inline-block;
}
.${prefixCls}::before {
display: none;
}
.${prefixCls}[tabindex] {
cursor: pointer;
}
.${prefixCls}-spin::before,
.${prefixCls}-spin {
display: inline-block;
-webkit-animation: loadingCircle 1s infinite linear;
animation: loadingCircle 1s infinite linear;
}
@-webkit-keyframes loadingCircle {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes loadingCircle {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
`
}
7 changes: 2 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e7b7ea2

Please sign in to comment.