Skip to content

Commit

Permalink
feat(other): support ssr (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
anlyyao authored Mar 29, 2024
1 parent fdb8279 commit a1e811f
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/action-sheet/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApp, DefineComponent, ref, h, VNode, App, nextTick } from 'vue';
import ActionSheetVue from './action-sheet.vue';
import { WithInstallType } from '../shared';
import { WithInstallType, isBrowser } from '../shared';

import './style';
import { TdActionSheetProps } from './type';
Expand All @@ -12,6 +12,8 @@ let instance: any = null;
let app: App<Element>;

function create(props: Partial<TdActionSheetProps>): DefineComponent<TdActionSheetProps> {
if (!isBrowser) return;

const root = document.createElement('div');
document.body.appendChild(root);

Expand Down
9 changes: 6 additions & 3 deletions src/back-top/back-top.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { computed, defineComponent, getCurrentInstance, h } from 'vue';
import { useElementBounding } from '@vueuse/core';
import { BacktopIcon as TIconBackTop } from 'tdesign-icons-vue-next';
import { renderTNode, TNode } from '../shared';
import { renderTNode, TNode, isBrowser } from '../shared';
import BackTopProps from './props';
import config from '../config';
Expand All @@ -22,6 +22,7 @@ export default defineComponent({
props: BackTopProps,
setup(props, context) {
const el = computed(() => {
if (!isBrowser) return undefined;
return props.target ? props.target() : window.document.documentElement;
});
const { top } = useElementBounding(el);
Expand All @@ -43,8 +44,10 @@ export default defineComponent({
});
const clickBackBtn = () => {
window.document.documentElement.scrollTop += top.value;
props.onToTop?.();
if (isBrowser) {
window.document.documentElement.scrollTop += top.value;
props.onToTop?.();
}
};
return {
name,
Expand Down
4 changes: 3 additions & 1 deletion src/dialog/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createApp, h, App, ref, nextTick, reactive } from 'vue';

import Dialog from './dialog.vue';
import { WithInstallType } from '../shared';
import { WithInstallType, isBrowser } from '../shared';
import { DialogCloseContext, TdDialogProps, DialogInstance } from './type';

import './style';
Expand All @@ -24,6 +24,8 @@ const propsFn = ['onConfirm', 'onCancel', 'onOverlayClick', 'onClose', 'onClosed
type DialogPropsFnName = (typeof propsFn)[number];

function create(options: Partial<TdDialogProps> | string): DialogInstance {
if (!isBrowser) return;

const root = document.createElement('div');
document.body.appendChild(root);

Expand Down
3 changes: 2 additions & 1 deletion src/drawer/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createApp, App, h, ref, nextTick } from 'vue';
import vueDrawer from './drawer.vue';
import { WithInstallType } from '../shared';
import { WithInstallType, isBrowser } from '../shared';
import { TdDrawerProps } from './type';

type DrawerOptions = Omit<TdDrawerProps, 'attach'>;

const Drawer = (options: DrawerOptions) => {
if (!isBrowser) return;
const root = document.createElement('div');
document.body.appendChild(root);
const visible = ref(false);
Expand Down
4 changes: 2 additions & 2 deletions src/image-viewer/image-viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import { CloseIcon, DeleteIcon } from 'tdesign-icons-vue-next';

import config from '../config';
import ImagediverProps from './props';
import { renderTNode, TNode, useDefault, inBrowser, useGesture, DragState, PinchState } from '../shared';
import { renderTNode, TNode, useDefault, isBrowser, useGesture, DragState, PinchState } from '../shared';

// inner components
import { Swiper as TSwiper, SwiperItem as TSwiperItem } from '../swiper';
Expand Down Expand Up @@ -218,7 +218,7 @@ export default defineComponent({
const checkTap = (e: DragState) => {
const { event } = e;
const deltaTime = Date.now() - dragStartTime;
if (deltaTime < TAP_TIME && inBrowser) {
if (deltaTime < TAP_TIME && isBrowser) {
if (dblTapTimer) {
clearTimeout(dblTapTimer);
dblTapTimer = window.setTimeout(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/message/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApp, defineComponent, ref, h, VNode, App, nextTick } from 'vue';
import Message from './message.vue';
import { WithInstallType } from '../shared';
import { WithInstallType, isBrowser } from '../shared';
import { TdMessageProps, MessageThemeList } from './type';

import './style';
Expand All @@ -21,6 +21,8 @@ function destroy(context: Element, root: Element) {
}

function create(props: MessageActionOptionsType): void {
if (!isBrowser) return;

const { context, ...otherOptions } = props;

if (!context) {
Expand Down
3 changes: 1 addition & 2 deletions src/picker/picker-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export default defineComponent({
emits: ['pick'],
setup(props, context) {
let picker: Picker | null = null;
const el = document.createElement('ul');
const root = ref(el);
const root = ref();
const getIndexByValue = (val: number | string | undefined) => {
let defaultIndex = 0;
if (val !== undefined) {
Expand Down
11 changes: 7 additions & 4 deletions src/popup/popup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import popupProps from './props';
import TOverlay from '../overlay';
import config from '../config';
import { TdPopupProps } from './type';
import { useDefault, TNode, renderTNode } from '../shared';
import { useDefault, TNode, renderTNode, isBrowser } from '../shared';
import { getAttach } from '../shared/dom';
const { prefix } = config;
Expand Down Expand Up @@ -109,7 +109,10 @@ export default defineComponent({
props.onClosed?.();
};
const afterEnter = () => props.onOpened?.();
const to = computed(() => getAttach(props.attach ?? 'body'));
const to = computed(() => {
if (!isBrowser || !props.attach) return undefined;
return getAttach(props.attach ?? 'body');
});
watch(
() => currentVisible.value,
Expand All @@ -122,7 +125,7 @@ export default defineComponent({
);
const lock = () => {
if (!lockTimes) {
if (!lockTimes && isBrowser) {
document.body.classList.add(bodyLockClass);
}
Expand All @@ -133,7 +136,7 @@ export default defineComponent({
if (lockTimes) {
lockTimes--;
if (!lockTimes) {
if (!lockTimes && isBrowser) {
document.body.classList.remove(bodyLockClass);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/shared/dom.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import isFunction from 'lodash/isFunction';
import isString from 'lodash/isString';

import { AttachNode } from '../common';
import { isBrowser } from './util';

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

export function getAttach(node: AttachNode) {
if (!isBrowser) return;

const attachNode = isFunction(node) ? node() : node;

if (isString(attachNode)) {
Expand Down
1 change: 0 additions & 1 deletion src/shared/render-tnode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
const TNodeComponent = (props: { content: any }) => props.content;
TNodeComponent.props = ['content'];
export default TNodeComponent;
4 changes: 2 additions & 2 deletions src/shared/useClickAway/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MaybeElementRef, UnRefElementReturn, unrefElement, useEventListener } from '@vueuse/core';
import isArray from 'lodash/isArray';
import { inBrowser } from '../util';
import { isBrowser } from '../util';

export interface UseClickAwayOptions {
/**
Expand Down Expand Up @@ -39,7 +39,7 @@ export function useClickAway<T extends UseClickAwayOptions>(
fn: UseClickAwayHandler<{ detectIframe: T['detectIframe'] }>,
options: T = {} as T,
) {
if (!inBrowser) return;
if (!isBrowser) return;

const { eventName = 'touchstart', capture = true, ignore = [], detectIframe = false } = options;

Expand Down
2 changes: 2 additions & 0 deletions src/shared/useCountDown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ref, reactive } from 'vue';
import { useRafFn } from '@vueuse/core';
import { TdUseCountDownProps, TdUseCountDown } from './type';
import { getRemainTimes, getShowTimes, getScreenFps } from './utils';
import { isBrowser } from '../util';

export function useCountDown(props: TdUseCountDownProps): TdUseCountDown {
const {
Expand All @@ -21,6 +22,7 @@ export function useCountDown(props: TdUseCountDownProps): TdUseCountDown {
// raf
const { pause, resume } = useRafFn(
async () => {
if (!isBrowser) return;
if (!fps.value) {
const res = await getScreenFps?.();
fps.value = res || 60;
Expand Down
2 changes: 2 additions & 0 deletions src/shared/useCountDown/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TimeData, TdUseCountDownShowTimes } from './type';
import { isBrowser } from '../util';

export const TimeDataUnit = {
DD: '天',
Expand Down Expand Up @@ -111,6 +112,7 @@ export const getShowTimes = (
* @return {Promise<number>}
*/
export const getScreenFps = (() => {
if (!isBrowser) return;
const { requestAnimationFrame, mozRequestAnimationFrame, webkitRequestAnimationFrame } = window as any;
// 先做一下兼容性处理
const nextFrame = [requestAnimationFrame, mozRequestAnimationFrame, webkitRequestAnimationFrame]?.find?.((fn) => fn);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/useScrollParent/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ref, Ref, onMounted } from 'vue';
import { inBrowser } from '../util';
import { isBrowser } from '../util';

type ScrollElement = HTMLElement | Window;

const overflowScrollReg = /scroll|auto/i;
const defaultRoot = inBrowser ? window : undefined;
const defaultRoot = isBrowser ? window : undefined;

function isElement(node: Element) {
const ELEMENT_NODE_TYPE = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function toCamel(str: string): string {
return str.replace(/^\S/, (m) => m.toUpperCase());
}

export const inBrowser = typeof window !== 'undefined';
export const isBrowser = typeof window !== 'undefined';

/**
* 计算字符串字符的长度并可以截取字符串。
Expand Down
5 changes: 3 additions & 2 deletions src/swipe-cell/useSwipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { computed, reactive, ref } from 'vue';
import { useEventListener } from '@vueuse/core';
import isObject from 'lodash/isObject';
import { preventDefault } from '../shared/dom';
import { isBrowser } from '../shared';

const noop = () => {};

Expand Down Expand Up @@ -105,7 +106,7 @@ export function useSwipe(
coordsEnd.y = y;
};

const isPassiveEventSupported = checkPassiveEventSupport(window?.document);
const isPassiveEventSupported = checkPassiveEventSupport();

const onTouchEnd = (e: TouchEvent) => {
if (isSwiping.value) onSwipeEnd?.(e, direction.value);
Expand Down Expand Up @@ -167,7 +168,7 @@ export function useSwipe(
* This is a polyfill for passive event support detection
* @see https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
*/
function checkPassiveEventSupport(document?: Document) {
function checkPassiveEventSupport(document = isBrowser ? window.document : undefined) {
if (!document) return false;
let supportsPassive = false;
const optionsBlock: AddEventListenerOptions = {
Expand Down
3 changes: 2 additions & 1 deletion src/toast/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createApp, App, DefineComponent } from 'vue';
import vueToast from './toast.vue';
import { TdToastProps } from './type';
import { WithInstallType } from '../shared';
import { WithInstallType, isBrowser } from '../shared';

import './style';

Expand All @@ -13,6 +13,7 @@ let app: App<Element>;

/** 展示提示 */
function Toast(props: string | Partial<TdToastProps>): DefineComponent<TdToastProps> {
if (!isBrowser) return;
const root = document.createElement('div');
document.body.appendChild(root);

Expand Down

0 comments on commit a1e811f

Please sign in to comment.