2020-08-31 16:53:19 +08:00
|
|
|
import classNames from '../_util/classNames';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { PropType, ExtractPropTypes, CSSProperties } from 'vue';
|
2022-03-21 14:36:16 +08:00
|
|
|
import { inject, defineComponent, ref, watch, onMounted, onBeforeUnmount, provide } from 'vue';
|
2020-03-07 19:45:13 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-10-15 15:54:52 +08:00
|
|
|
import { tuple } from '../_util/type';
|
|
|
|
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
2020-03-07 19:45:13 +08:00
|
|
|
import isNumeric from '../_util/isNumeric';
|
2020-03-26 11:59:35 +08:00
|
|
|
import BarsOutlined from '@ant-design/icons-vue/BarsOutlined';
|
|
|
|
import RightOutlined from '@ant-design/icons-vue/RightOutlined';
|
|
|
|
import LeftOutlined from '@ant-design/icons-vue/LeftOutlined';
|
2021-06-07 17:35:03 +08:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import { SiderCollapsedKey, SiderHookProviderKey } from './injectionKey';
|
2020-03-07 19:45:13 +08:00
|
|
|
|
|
|
|
const dimensionMaxMap = {
|
|
|
|
xs: '479.98px',
|
|
|
|
sm: '575.98px',
|
|
|
|
md: '767.98px',
|
|
|
|
lg: '991.98px',
|
|
|
|
xl: '1199.98px',
|
|
|
|
xxl: '1599.98px',
|
2021-11-30 21:07:50 +08:00
|
|
|
xxxl: '1999.98px',
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2020-10-15 15:54:52 +08:00
|
|
|
export type CollapseType = 'clickTrigger' | 'responsive';
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
export const siderProps = {
|
2018-04-20 12:52:31 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2020-10-10 18:16:28 +08:00
|
|
|
collapsible: PropTypes.looseBool,
|
|
|
|
collapsed: PropTypes.looseBool,
|
|
|
|
defaultCollapsed: PropTypes.looseBool,
|
|
|
|
reverseArrow: PropTypes.looseBool,
|
2020-10-15 15:54:52 +08:00
|
|
|
zeroWidthTriggerStyle: PropTypes.style,
|
2021-12-16 17:20:18 +08:00
|
|
|
trigger: PropTypes.any,
|
2018-04-20 12:52:31 +08:00
|
|
|
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
|
|
|
collapsedWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
2021-11-30 21:07:50 +08:00
|
|
|
breakpoint: PropTypes.oneOf(tuple('xs', 'sm', 'md', 'lg', 'xl', 'xxl', 'xxxl')),
|
2020-10-15 15:54:52 +08:00
|
|
|
theme: PropTypes.oneOf(tuple('light', 'dark')).def('dark'),
|
|
|
|
onBreakpoint: Function as PropType<(broken: boolean) => void>,
|
|
|
|
onCollapse: Function as PropType<(collapsed: boolean, type: CollapseType) => void>,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
export type SiderProps = Partial<ExtractPropTypes<typeof siderProps>>;
|
2018-04-20 12:52:31 +08:00
|
|
|
// export interface SiderState {
|
|
|
|
// collapsed?: boolean;
|
|
|
|
// below: boolean;
|
|
|
|
// belowShow?: boolean;
|
|
|
|
// }
|
|
|
|
|
2020-10-24 22:38:25 +08:00
|
|
|
export interface SiderContextProps {
|
|
|
|
sCollapsed?: boolean;
|
|
|
|
collapsedWidth?: string | number;
|
|
|
|
}
|
2018-04-20 12:52:31 +08:00
|
|
|
|
|
|
|
const generateId = (() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
let i = 0;
|
2018-04-20 12:52:31 +08:00
|
|
|
return (prefix = '') => {
|
2019-01-12 11:33:27 +08:00
|
|
|
i += 1;
|
|
|
|
return `${prefix}${i}`;
|
|
|
|
};
|
|
|
|
})();
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2020-10-15 15:54:52 +08:00
|
|
|
export default defineComponent({
|
2018-04-20 12:52:31 +08:00
|
|
|
name: 'ALayoutSider',
|
2020-12-24 10:37:30 +08:00
|
|
|
inheritAttrs: false,
|
2021-06-07 17:35:03 +08:00
|
|
|
props: initDefaultProps(siderProps, {
|
2018-04-20 12:52:31 +08:00
|
|
|
collapsible: false,
|
|
|
|
defaultCollapsed: false,
|
|
|
|
reverseArrow: false,
|
|
|
|
width: 200,
|
|
|
|
collapsedWidth: 80,
|
|
|
|
}),
|
2020-10-15 15:54:52 +08:00
|
|
|
emits: ['breakpoint', 'update:collapsed', 'collapse'],
|
2021-06-07 17:35:03 +08:00
|
|
|
setup(props, { emit, attrs, slots }) {
|
|
|
|
const { prefixCls } = useConfigInject('layout-sider', props);
|
2021-06-20 22:53:42 +08:00
|
|
|
const siderHook = inject(SiderHookProviderKey, undefined);
|
2021-06-07 17:35:03 +08:00
|
|
|
const collapsed = ref(
|
|
|
|
!!(props.collapsed !== undefined ? props.collapsed : props.defaultCollapsed),
|
|
|
|
);
|
|
|
|
const below = ref(false);
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
watch(
|
|
|
|
() => props.collapsed,
|
|
|
|
() => {
|
|
|
|
collapsed.value = !!props.collapsed;
|
|
|
|
},
|
|
|
|
);
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
provide(SiderCollapsedKey, collapsed);
|
|
|
|
|
|
|
|
const handleSetCollapsed = (value: boolean, type: CollapseType) => {
|
|
|
|
if (props.collapsed === undefined) {
|
|
|
|
collapsed.value = value;
|
2018-04-20 12:52:31 +08:00
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
emit('update:collapsed', value);
|
|
|
|
emit('collapse', value, type);
|
|
|
|
};
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
// ========================= Responsive =========================
|
|
|
|
const responsiveHandlerRef = ref<(mql: MediaQueryListEvent | MediaQueryList) => void>(
|
|
|
|
(mql: MediaQueryListEvent | MediaQueryList) => {
|
|
|
|
below.value = mql.matches;
|
|
|
|
emit('breakpoint', mql.matches);
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
if (collapsed.value !== mql.matches) {
|
|
|
|
handleSetCollapsed(mql.matches, 'responsive');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let mql: MediaQueryList;
|
|
|
|
function responsiveHandler(mql: MediaQueryListEvent | MediaQueryList) {
|
|
|
|
return responsiveHandlerRef.value!(mql);
|
2018-04-20 12:52:31 +08:00
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
const uniqueId = generateId('ant-sider-');
|
2021-12-12 14:32:53 +08:00
|
|
|
siderHook && siderHook.addSider(uniqueId);
|
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
onMounted(() => {
|
2022-03-21 14:36:16 +08:00
|
|
|
watch(
|
|
|
|
() => props.breakpoint,
|
|
|
|
() => {
|
|
|
|
try {
|
|
|
|
mql?.removeEventListener('change', responsiveHandler);
|
|
|
|
} catch (error) {
|
|
|
|
mql?.removeListener(responsiveHandler);
|
|
|
|
}
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
const { matchMedia } = window;
|
|
|
|
if (matchMedia! && props.breakpoint && props.breakpoint in dimensionMaxMap) {
|
|
|
|
mql = matchMedia(`(max-width: ${dimensionMaxMap[props.breakpoint]})`);
|
|
|
|
try {
|
|
|
|
mql.addEventListener('change', responsiveHandler);
|
|
|
|
} catch (error) {
|
|
|
|
mql.addListener(responsiveHandler);
|
|
|
|
}
|
|
|
|
responsiveHandler(mql);
|
2022-03-17 10:32:58 +08:00
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
}
|
2022-03-21 14:36:16 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
immediate: true,
|
|
|
|
},
|
|
|
|
);
|
2021-06-07 17:35:03 +08:00
|
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
try {
|
|
|
|
mql?.removeEventListener('change', responsiveHandler);
|
|
|
|
} catch (error) {
|
|
|
|
mql?.removeListener(responsiveHandler);
|
2018-04-20 12:52:31 +08:00
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
siderHook && siderHook.removeSider(uniqueId);
|
|
|
|
});
|
2018-04-20 12:52:31 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
const toggle = () => {
|
|
|
|
handleSetCollapsed(!collapsed.value, 'clickTrigger');
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2021-06-07 17:35:03 +08:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
const pre = prefixCls.value;
|
|
|
|
const {
|
|
|
|
collapsedWidth,
|
|
|
|
width,
|
|
|
|
reverseArrow,
|
|
|
|
zeroWidthTriggerStyle,
|
2022-03-12 10:05:33 +08:00
|
|
|
trigger = slots.trigger?.(),
|
2021-06-07 17:35:03 +08:00
|
|
|
collapsible,
|
|
|
|
theme,
|
|
|
|
} = props;
|
|
|
|
const rawWidth = collapsed.value ? collapsedWidth : width;
|
|
|
|
// use "px" as fallback unit for width
|
|
|
|
const siderWidth = isNumeric(rawWidth) ? `${rawWidth}px` : String(rawWidth);
|
|
|
|
// special trigger when collapsedWidth == 0
|
|
|
|
const zeroWidthTrigger =
|
|
|
|
parseFloat(String(collapsedWidth || 0)) === 0 ? (
|
|
|
|
<span
|
|
|
|
onClick={toggle}
|
|
|
|
class={classNames(
|
|
|
|
`${pre}-zero-width-trigger`,
|
|
|
|
`${pre}-zero-width-trigger-${reverseArrow ? 'right' : 'left'}`,
|
|
|
|
)}
|
|
|
|
style={zeroWidthTriggerStyle}
|
|
|
|
>
|
|
|
|
{trigger || <BarsOutlined />}
|
|
|
|
</span>
|
|
|
|
) : null;
|
|
|
|
const iconObj = {
|
|
|
|
expanded: reverseArrow ? <RightOutlined /> : <LeftOutlined />,
|
|
|
|
collapsed: reverseArrow ? <LeftOutlined /> : <RightOutlined />,
|
|
|
|
};
|
|
|
|
const status = collapsed.value ? 'collapsed' : 'expanded';
|
|
|
|
const defaultTrigger = iconObj[status];
|
|
|
|
const triggerDom =
|
|
|
|
trigger !== null
|
|
|
|
? zeroWidthTrigger || (
|
|
|
|
<div class={`${pre}-trigger`} onClick={toggle} style={{ width: siderWidth }}>
|
|
|
|
{trigger || defaultTrigger}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
const divStyle = {
|
|
|
|
...(attrs.style as CSSProperties),
|
|
|
|
flex: `0 0 ${siderWidth}`,
|
|
|
|
maxWidth: siderWidth, // Fix width transition bug in IE11
|
|
|
|
minWidth: siderWidth, // https://github.com/ant-design/ant-design/issues/6349
|
|
|
|
width: siderWidth,
|
|
|
|
};
|
|
|
|
const siderCls = classNames(
|
|
|
|
pre,
|
|
|
|
`${pre}-${theme}`,
|
|
|
|
{
|
|
|
|
[`${pre}-collapsed`]: !!collapsed.value,
|
|
|
|
[`${pre}-has-trigger`]: collapsible && trigger !== null && !zeroWidthTrigger,
|
|
|
|
[`${pre}-below`]: !!below.value,
|
|
|
|
[`${pre}-zero-width`]: parseFloat(siderWidth) === 0,
|
|
|
|
},
|
|
|
|
attrs.class,
|
|
|
|
);
|
|
|
|
return (
|
2021-12-11 22:29:29 +08:00
|
|
|
<aside {...attrs} class={siderCls} style={divStyle}>
|
2021-06-07 17:35:03 +08:00
|
|
|
<div class={`${pre}-children`}>{slots.default?.()}</div>
|
|
|
|
{collapsible || (below.value && zeroWidthTrigger) ? triggerDom : null}
|
|
|
|
</aside>
|
|
|
|
);
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-04-20 12:52:31 +08:00
|
|
|
},
|
2020-10-15 15:54:52 +08:00
|
|
|
});
|