Skip to content

Commit ba3a8f5

Browse files
Fix the cross-origin access issue of stylesheets (GH-38)
2 parents c06748a + 9e16561 commit ba3a8f5

File tree

3 files changed

+41
-49
lines changed

3 files changed

+41
-49
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.7",
2+
"version": "0.1.8",
33
"name": "antd-phone-input",
44
"description": "Advanced, highly customizable phone input component for Ant Design.",
55
"keywords": [

src/index.tsx

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,36 @@
1-
import {useContext, useEffect, useMemo} from "react";
2-
import theme from "antd/lib/theme";
1+
import {useContext, useMemo} from "react";
2+
import useToken from "antd/lib/theme/useToken";
33
import genComponentStyleHook from "antd/lib/input/style";
4+
import {ConfigContext} from "antd/lib/config-provider";
5+
import {useStyleRegister} from "antd/lib/theme/internal";
46
import {FormItemInputContext} from "antd/lib/form/context";
57
import {getStatusClassNames} from "antd/lib/_util/statusUtils";
68

79
import InputLegacy from "./legacy";
10+
import genPhoneInputStyle from "./style";
811
import {PhoneInputProps} from "./types";
912

1013
const PhoneInput = (inputLegacyProps: PhoneInputProps) => {
11-
const {token} = theme.useToken();
14+
const {getPrefixCls} = useContext(ConfigContext);
1215
const {status}: any = useContext(FormItemInputContext);
13-
const [_1, inputCls] = genComponentStyleHook("ant-input");
14-
const [_2, dropdownCls] = genComponentStyleHook("ant-dropdown");
16+
const inputPrefixCls = getPrefixCls("input");
17+
const dropdownPrefixCls = getPrefixCls("dropdown");
18+
const [_1, inputCls] = genComponentStyleHook(inputPrefixCls);
19+
const [_2, dropdownCls] = genComponentStyleHook(dropdownPrefixCls);
20+
const [theme, token, _3] = useToken();
1521

1622
const inputClass = useMemo(() => {
17-
return `${inputCls} ` + getStatusClassNames("ant-input", status);
18-
}, [inputCls, status]);
23+
return `${inputCls} ` + getStatusClassNames(inputPrefixCls, status);
24+
}, [inputPrefixCls, inputCls, status]);
1925

2026
const dropdownClass = useMemo(() => "ant-dropdown " + dropdownCls, [dropdownCls]);
2127

22-
useEffect(() => {
23-
/** Load antd 5.x styles dynamically observing the theme change */
24-
for (let styleSheet of document?.styleSheets || []) {
25-
let rule: any;
26-
for (rule of styleSheet?.cssRules || styleSheet?.rules || []) {
27-
if (rule.selectorText === ".react-tel-input .country-list") {
28-
rule.style.boxShadow = token.boxShadow;
29-
rule.style.backgroundColor = token.colorBgElevated;
30-
}
31-
if (rule.selectorText === ".react-tel-input .selected-flag") {
32-
rule.style.borderColor = token.colorBorder;
33-
}
34-
if (rule.selectorText === ".react-tel-input .country-list .search") {
35-
rule.style.backgroundColor = token.colorBgElevated;
36-
}
37-
if (rule.selectorText === ".react-tel-input .country-list .country") {
38-
rule.style.borderRadius = token.borderRadiusOuter + "px";
39-
}
40-
if (rule.selectorText === ".react-tel-input .country-list .country-name") {
41-
rule.style.color = token.colorText;
42-
}
43-
if (rule.selectorText === ".react-tel-input .country-list .country .dial-code") {
44-
rule.style.color = token.colorTextDescription;
45-
}
46-
if (rule.selectorText === ".react-tel-input .country-list .country:hover") {
47-
rule.style.backgroundColor = token.colorBgTextHover;
48-
}
49-
if (rule.selectorText === ".react-tel-input .country-list .country.highlight") {
50-
rule.style.backgroundColor = token.colorPrimaryBg;
51-
}
52-
if (rule.selectorText === `:where(.${inputCls}).ant-input`) {
53-
rule.selectorText += "\n,.react-tel-input .country-list .search-box";
54-
rule.style.backgroundColor = token.colorBgElevated;
55-
}
56-
if (rule.selectorText === `:where(.${inputCls}).ant-input:hover`) {
57-
rule.selectorText += "\n,.react-tel-input .country-list .search-box:focus";
58-
rule.selectorText += "\n,.react-tel-input .country-list .search-box:hover";
59-
}
60-
}
61-
}
62-
}, [inputCls, token])
28+
useStyleRegister({
29+
theme,
30+
token,
31+
hashId: "react-tel-input",
32+
path: ["antd-phone-input"],
33+
}, () => [genPhoneInputStyle(token)]);
6334

6435
return (
6536
<InputLegacy

src/style.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {GlobalToken} from "antd/lib/theme/interface";
2+
import {genBasicInputStyle, initInputToken, InputToken} from "antd/lib/input/style";
3+
4+
export default (token: GlobalToken) => ({
5+
".react-tel-input": {
6+
".country-list": {
7+
boxShadow: token.boxShadow,
8+
backgroundColor: token.colorBgElevated,
9+
".country-name": {color: token.colorText},
10+
".search": {backgroundColor: token.colorBgElevated},
11+
".search-box": genBasicInputStyle(initInputToken(token) as InputToken),
12+
".country": {
13+
borderRadius: token.borderRadiusOuter,
14+
".dial-code": {color: token.colorTextDescription},
15+
"&:hover": {backgroundColor: token.colorBgTextHover},
16+
"&.highlight": {backgroundColor: token.colorPrimaryBg},
17+
},
18+
},
19+
".selected-flag": {borderColor: token.colorBorder},
20+
}
21+
})

0 commit comments

Comments
 (0)