Skip to content

Commit

Permalink
fix: 修复在ssr环境下的报错 (#2417)
Browse files Browse the repository at this point in the history
* fix: 修复在ssr环境下的报错

* fix: nuxt error

* chore: lint fix
  • Loading branch information
PengYYYYY authored Feb 18, 2023
1 parent 5291b96 commit e67cec4
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { stack } from './stack';
import { getAttach, getScrollbarWidth, getSSRAttach } from '../utils/dom';

import type { TdDialogProps } from './type';
import isUndefined from 'lodash/isUndefined';

function GetCSSValue(v: string | number) {
return Number.isNaN(Number(v)) ? v : `${Number(v)}px`;
Expand All @@ -45,7 +44,7 @@ const getClickPosition = (e: MouseEvent) => {
}, 100);
};

if (!isUndefined(window) && window.document && window.document.documentElement) {
if (typeof window !== 'undefined' && window.document && window.document.documentElement) {
document.documentElement.addEventListener('click', getClickPosition, true);
}

Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useResizeObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export default function useResizeObserver(
container: Ref<HTMLElement>,
callback: (data: ResizeObserverEntry[]) => void,
) {
const isSupport = window && window.ResizeObserver;
if (typeof window === 'undefined') return;

const isSupport = window && (window as Window & typeof globalThis).ResizeObserver;
// unit tests do not need any warn console; too many warns influence focusing on more important log info
if (!isSupport) return;

Expand Down
2 changes: 2 additions & 0 deletions src/input/useInputWidth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default function useInputWidth(
const resizeObserver = ref<ResizeObserver>(null);
// 当元素默认为 display: none 状态,无法提前准确计算宽度,因此需要监听元素宽度变化。比如:Tabs 场景切换。
const addTableResizeObserver = (element: Element) => {
if (typeof window === 'undefined') return;

// IE 11 以下使用设置 minWidth 兼容;IE 11 以上使用 ResizeObserver
if (isUndefined(window.ResizeObserver) || !element) return;
resizeObserver.value = new window.ResizeObserver(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/textarea/calcTextareaHeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const SIZING_PROPS = [
];

function calculateNodeStyling(targetElement: HTMLTextAreaElement) {
if (typeof window === 'undefined') return;

const style = window.getComputedStyle(targetElement);

const boxSizing =
Expand Down
1 change: 1 addition & 0 deletions src/tree/hooks/useDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function useDraggable(props: { nodeRef: HTMLElement | undefined;
const dropPosition = ref<DropPosition>(0);

const updateDropPosition = throttle((e: DragEvent) => {
if (typeof window === 'undefined') return;
if (!nodeRef.value) return;

const rect = nodeRef.value.getBoundingClientRect();
Expand Down
3 changes: 1 addition & 2 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import { ComponentPublicInstance, VNode } from 'vue';
import raf from 'raf';
import isString from 'lodash/isString';
import isUndefined from 'lodash/isUndefined';
import isFunction from 'lodash/isFunction';
import isArray from 'lodash/isArray';
import { easeInOutCubic, EasingFunction } from './easing';
import { ScrollContainer, ScrollContainerElement } from '../common';

export const isServer = isUndefined(window);
export const isServer = typeof window === 'undefined';
const trim = (str: string): string => (str || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');

export const on = ((): any => {
Expand Down
3 changes: 1 addition & 2 deletions src/watermark/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import isUndefined from 'lodash/isUndefined';
import type { ComponentPublicInstance, Ref } from 'vue';
import { unref, watch, getCurrentScope, onScopeDispose } from 'vue';

export const defaultWindow = !isUndefined(window) ? window : undefined;
export const defaultWindow = typeof window !== 'undefined' ? window : undefined;
export interface ConfigurableWindow {
window?: Window;
}
Expand Down

0 comments on commit e67cec4

Please sign in to comment.