ant-design-vue/components/anchor/context.ts
Cherry7 8932aff13f
feat(anchor): add direction action (#6447)
* refactor(anchor): direction show

* refactor(anchor): update anchor css

* feat(anchor): update demo

* test(anchor): update demo test snap

* feat(anchor): update docs

* Update index.zh-CN.md

* Update index.en-US.md

---------

Co-authored-by: tangjinzhou <415800467@qq.com>
2023-04-21 17:33:38 +08:00

36 lines
1.1 KiB
TypeScript

import type { Ref, InjectionKey, ComputedRef } from 'vue';
import type { AnchorDirection } from './Anchor';
import { computed, inject, provide } from 'vue';
export interface AnchorContext {
registerLink: (link: string) => void;
unregisterLink: (link: string) => void;
activeLink: Ref<string>;
scrollTo: (link: string) => void;
handleClick: (e: Event, info: { title: any; href: string }) => void;
direction: ComputedRef<AnchorDirection>;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function noop(..._any: any[]): any {}
export const AnchorContextKey: InjectionKey<AnchorContext> = Symbol('anchorContextKey');
const useProvideAnchor = (state: AnchorContext) => {
provide(AnchorContextKey, state);
};
const useInjectAnchor = () => {
return inject(AnchorContextKey, {
registerLink: noop,
unregisterLink: noop,
scrollTo: noop,
activeLink: computed(() => ''),
handleClick: noop,
direction: computed(() => 'vertical'),
} as AnchorContext);
};
export { useInjectAnchor, useProvideAnchor };
export default useProvideAnchor;