@4399ywkf/core 5.0.19 → 5.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3594,12 +3594,18 @@ var themePlugin = createPlugin((options = {}) => ({
3594
3594
  order: 10,
3595
3595
  }`;
3596
3596
  if (code.includes("providers: []")) {
3597
- code = code.replace("providers: []", `providers: [
3597
+ code = code.replace(
3598
+ "providers: []",
3599
+ `providers: [
3598
3600
  ${providerEntry},
3599
- ]`);
3601
+ ]`
3602
+ );
3600
3603
  } else if (code.includes("providers: [")) {
3601
- code = code.replace("providers: [", `providers: [
3602
- ${providerEntry},`);
3604
+ code = code.replace(
3605
+ "providers: [",
3606
+ `providers: [
3607
+ ${providerEntry},`
3608
+ );
3603
3609
  }
3604
3610
  if (!code.includes("import React")) {
3605
3611
  code = code.replace(
@@ -3629,13 +3635,23 @@ function generateThemeProvider(opts) {
3629
3635
  sections.push(`// \u6B64\u6587\u4EF6\u7531 @4399ywkf/plugin-theme v3 \u81EA\u52A8\u751F\u6210\uFF0C\u8BF7\u52FF\u624B\u52A8\u4FEE\u6539`);
3630
3636
  sections.push(buildImports({ globalReset, cssVar }));
3631
3637
  sections.push(TYPES_CODE);
3632
- sections.push(buildStoreCode({ defaultAppearance, primaryColor, neutralColor }));
3638
+ sections.push(
3639
+ buildStoreCode({ defaultAppearance, primaryColor, neutralColor })
3640
+ );
3633
3641
  sections.push(HOOKS_CODE);
3634
3642
  if (globalReset) sections.push(GLOBAL_RESET_CODE);
3635
- if (cssVar) sections.push(CSS_VAR_SYNC_CODE);
3643
+ if (cssVar) sections.push(buildCssVarSyncCode());
3636
3644
  if (darkMode) sections.push(APPEARANCE_SYNC_CODE);
3637
3645
  if (externalTheme) sections.push(EXTERNAL_THEME_CODE);
3638
- sections.push(buildWrapperCode({ darkMode, cssVar, globalReset, externalTheme, prefixCls }));
3646
+ sections.push(
3647
+ buildWrapperCode({
3648
+ darkMode,
3649
+ cssVar,
3650
+ globalReset,
3651
+ externalTheme,
3652
+ prefixCls
3653
+ })
3654
+ );
3639
3655
  return sections.join("\n");
3640
3656
  }
3641
3657
  function buildImports(opts) {
@@ -3643,11 +3659,13 @@ function buildImports(opts) {
3643
3659
  "useCallback",
3644
3660
  "useEffect",
3645
3661
  "useMemo",
3662
+ "useState",
3646
3663
  ...opts.cssVar ? ["useLayoutEffect", "useRef"] : [],
3647
3664
  "type ReactNode"
3648
3665
  ];
3649
3666
  const antdStyleImports = [
3650
3667
  "ThemeProvider as AntdThemeProvider",
3668
+ "StyleProvider",
3651
3669
  "type GetAntdTheme",
3652
3670
  ...opts.globalReset ? ["createGlobalStyle", "css"] : []
3653
3671
  ];
@@ -3777,10 +3795,15 @@ const GlobalReset = createGlobalStyle(({ theme }) => css\`
3777
3795
  -webkit-text-fill-color: unset !important;
3778
3796
  }
3779
3797
  \`);`;
3780
- var CSS_VAR_SYNC_CODE = `
3798
+ function buildCssVarSyncCode() {
3799
+ return `
3781
3800
  // \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 CSS Variable Sync \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
3782
3801
 
3783
- const CSS_VAR_PREFIX = "css-var-";
3802
+ const CSS_VAR_CLASS_RE = /(^|-)css-var(-|$)/;
3803
+
3804
+ function isCssVarClassName(className: string): boolean {
3805
+ return CSS_VAR_CLASS_RE.test(className);
3806
+ }
3784
3807
 
3785
3808
  function useCssVarSync(ref: React.RefObject<HTMLDivElement | null>) {
3786
3809
  useLayoutEffect(() => {
@@ -3791,18 +3814,28 @@ function useCssVarSync(ref: React.RefObject<HTMLDivElement | null>) {
3791
3814
  let currentClasses: string[] = [];
3792
3815
 
3793
3816
  const sync = () => {
3794
- for (const cls of currentClasses) htmlEl.classList.remove(cls);
3817
+ for (const cls of currentClasses) {
3818
+ htmlEl.classList.remove(cls);
3819
+ }
3795
3820
 
3796
- const next: string[] = [];
3821
+ const nextSet = new Set<string>();
3797
3822
  let el: HTMLElement | null = node;
3823
+
3798
3824
  while (el && el !== htmlEl) {
3799
3825
  for (const cls of el.classList) {
3800
- if (cls.startsWith(CSS_VAR_PREFIX)) next.push(cls);
3826
+ if (isCssVarClassName(cls)) {
3827
+ nextSet.add(cls);
3828
+ }
3801
3829
  }
3802
3830
  el = el.parentElement;
3803
3831
  }
3804
3832
 
3805
- for (const cls of next) htmlEl.classList.add(cls);
3833
+ const next = Array.from(nextSet);
3834
+
3835
+ for (const cls of next) {
3836
+ htmlEl.classList.add(cls);
3837
+ }
3838
+
3806
3839
  currentClasses = next;
3807
3840
  };
3808
3841
 
@@ -3817,10 +3850,13 @@ function useCssVarSync(ref: React.RefObject<HTMLDivElement | null>) {
3817
3850
 
3818
3851
  return () => {
3819
3852
  observer.disconnect();
3820
- for (const cls of currentClasses) htmlEl.classList.remove(cls);
3853
+ for (const cls of currentClasses) {
3854
+ htmlEl.classList.remove(cls);
3855
+ }
3821
3856
  };
3822
3857
  }, []);
3823
3858
  }`;
3859
+ }
3824
3860
  var APPEARANCE_SYNC_CODE = `
3825
3861
  // \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 Appearance Sync \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
3826
3862
 
@@ -3870,9 +3906,21 @@ function buildWrapperCode(opts) {
3870
3906
  const childrenSlot = cssVar ? `<div ref={containerRef} style={{ display: "contents" }}>
3871
3907
  {children}
3872
3908
  </div>` : "{children}";
3909
+ const antdProvider = `<AntdThemeProvider
3910
+ prefixCls={RUNTIME_PREFIX_CLS}
3911
+ appearance={resolvedAppearance}
3912
+ themeMode={appearance}
3913
+ theme={theme}${cssVar ? "\n customToken={{ cssVar: true }}" : ""}
3914
+ >
3915
+ ${globalReset ? "<GlobalReset />" : ""}
3916
+ ${childrenSlot}
3917
+ </AntdThemeProvider>`;
3873
3918
  return `
3874
3919
  // \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 ThemeWrapper \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
3875
3920
 
3921
+ // \u8FD0\u884C\u65F6\u5224\u65AD\u662F\u5426\u5904\u4E8E Garfish \u5B50\u5E94\u7528\u73AF\u5883\uFF0C\u540C\u4E00 bundle \u652F\u6301\u72EC\u7ACB\u90E8\u7F72\u4E0E\u5FAE\u524D\u7AEF\u4E24\u79CD\u6A21\u5F0F\u3002
3922
+ const IS_MICRO_APP = typeof window !== "undefined" && !!(window as any).__GARFISH__;
3923
+
3876
3924
  const RUNTIME_PREFIX_CLS = typeof process !== "undefined"
3877
3925
  && process.env?.YWKF_PREFIX_CLS
3878
3926
  || "${prefixCls}";
@@ -3887,6 +3935,21 @@ export function ThemeWrapper({ children }: ThemeWrapperProps) {${refLine}
3887
3935
  shallow,
3888
3936
  );${cssVarSyncLine}${appearanceSyncLine}${externalThemeLine}
3889
3937
 
3938
+ // \u5FAE\u524D\u7AEF\u6A21\u5F0F\u4E0B\uFF0C\u5C06 CSS-in-JS \u6837\u5F0F\u6CE8\u5165\u5230 Garfish \u5B50\u5E94\u7528\u5BB9\u5668\u800C\u975E document.head\uFF0C
3939
+ // \u907F\u514D\u4E0E\u4E3B\u5E94\u7528\u5171\u4EAB\u7684 @ant-design/cssinjs StyleContext \u4EA7\u751F\u51B2\u7A81\u3002
3940
+ // useState lazy initializer \u540C\u6B65\u8BFB\u53D6\uFF08Garfish \u6302\u8F7D\u65F6\u5BB9\u5668\u5DF2\u63D0\u524D\u521B\u5EFA\uFF09\uFF0C
3941
+ // useEffect \u515C\u5E95\u5904\u7406\u6781\u7AEF\u7ADE\u6001\uFF08\u5982 SSR hydration\uFF09\u3002
3942
+ const [styleContainer, setStyleContainer] = useState<HTMLElement | undefined>(() => {
3943
+ if (!IS_MICRO_APP || typeof document === "undefined") return undefined;
3944
+ return document.getElementById(RUNTIME_PREFIX_CLS)?.parentElement ?? undefined;
3945
+ });
3946
+
3947
+ useEffect(() => {
3948
+ if (!IS_MICRO_APP) return;
3949
+ const el = document.getElementById(RUNTIME_PREFIX_CLS)?.parentElement;
3950
+ if (el) setStyleContainer(el);
3951
+ }, []);
3952
+
3890
3953
  const resolvedAppearance = useMemo(() => {
3891
3954
  if (appearance !== "auto") return appearance;
3892
3955
  if (typeof window === "undefined") return "light";
@@ -3902,17 +3965,13 @@ export function ThemeWrapper({ children }: ThemeWrapperProps) {${refLine}
3902
3965
  [primaryColor, neutralColor],
3903
3966
  );
3904
3967
 
3905
- return (
3906
- <AntdThemeProvider
3907
- prefixCls={RUNTIME_PREFIX_CLS}
3908
- appearance={resolvedAppearance}
3909
- themeMode={appearance}
3910
- theme={theme}${cssVar ? "\n customToken={{ cssVar: true }}" : ""}
3911
- >
3912
- ${globalReset ? "<GlobalReset />" : ""}
3913
- ${childrenSlot}
3914
- </AntdThemeProvider>
3968
+ const provider = (
3969
+ ${antdProvider}
3915
3970
  );
3971
+
3972
+ return IS_MICRO_APP && styleContainer ? (
3973
+ <StyleProvider container={styleContainer}>{provider}</StyleProvider>
3974
+ ) : provider;
3916
3975
  }
3917
3976
 
3918
3977
  export default ThemeWrapper;