Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defineClientComponent doesn't forward slots #3867

Open
4 tasks done
linpengteng opened this issue May 6, 2024 · 2 comments
Open
4 tasks done

defineClientComponent doesn't forward slots #3867

linpengteng opened this issue May 6, 2024 · 2 comments
Labels
enhancement New feature or request has-workaround Has workaround, low priority

Comments

@linpengteng
Copy link

Describe the bug

当我将一个组件全局注册后,在 Markdown 使用该组件时,发现渲染的是: 渲染的是默认的值, 而不是 希望渲染的是来自于这里 Slot 定义的内容, 这不是我们想要的

image

Reproduction

Bug 重现:
https://stackblitz.com/edit/vite-7waswa?file=docs%2Fbug.md

Expected behavior

当我将一个组件全局注册后,在 Markdown 使用该组件时,希望也能接受并渲染来自于父组件的 Slot 定义

image

System Info

System:
    OS: macOS 14.2.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 3.31 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
    pnpm: 9.0.6 - /usr/local/bin/pnpm
    bun: 1.1.7 - /usr/local/bin/bun
  Browsers:
    Chrome: 124.0.6367.119
    Safari: 17.2.1
  npmPackages:
    vitepress: ^1.1.4 => 1.1.4

Additional context

No response

Validations

@linpengteng linpengteng added the bug: pending triage Maybe a bug, waiting for confirmation label May 6, 2024
@brc-dd
Copy link
Member

brc-dd commented May 6, 2024

defineClientComponent currently doesn't forward slots. You can do something like this:

import { h, shallowRef, onMounted } from 'vue';
import { Theme } from 'vitepress';
import DefaultTheme from 'vitepress/theme';

import './style.css';

export default {
  extends: DefaultTheme,
  Layout: () => h(DefaultTheme.Layout),
  enhanceApp({ app }) {
    app.component('CustomComponent', {
      setup(props, { slots }) {
        const comp = shallowRef();
        onMounted(async () => {
          let res = await import('../preset/CustomComponent.vue');
          if (res && (res.__esModule || res[Symbol.toStringTag] === 'Module')) {
            res = res.default;
          }
          comp.value = res;
        });
        return () => (comp.value ? h(comp.value, props, slots) : null);
      },
    });
  },
} satisfies Theme;

https://stackblitz.com/edit/vite-ajq8wc?file=docs/.vitepress/theme/index.ts

@brc-dd brc-dd added enhancement New feature or request has-workaround Has workaround, low priority and removed bug: pending triage Maybe a bug, waiting for confirmation labels May 6, 2024
@linpengteng
Copy link
Author

可以修复其问题,实现 Slots 功能。 但是感觉不是很优雅~

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 16, 2024
@vuejs vuejs unlocked this conversation Sep 23, 2024
@brc-dd brc-dd reopened this Sep 23, 2024
@brc-dd brc-dd changed the title 当我将一个组件全局注册后,在 Markdown 使用该组件时,发现 Slot 渲染有些问题 defineClientComponent doesn't forward slots Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request has-workaround Has workaround, low priority
Projects
None yet
Development

No branches or pull requests

2 participants