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

feat: pass all provides down to custom renderer #806

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

alvarosabu
Copy link
Member

@alvarosabu alvarosabu commented Aug 27, 2024

Closes #537
Closes #732

Copy link

netlify bot commented Aug 27, 2024

Deploy Preview for tresjs-docs ready!

Name Link
🔨 Latest commit 03c14f0
🔍 Latest deploy log https://app.netlify.com/sites/tresjs-docs/deploys/66e30b1b93ba0100078fd359
😎 Deploy Preview https://deploy-preview-806--tresjs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

pkg-pr-new bot commented Aug 27, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/@tresjs/core@806

commit: 03c14f0


// Extract provides from the current instance and merge them
if (currentInstance.provides) {
Object.assign(provides, currentInstance.provides)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precedence is backwards here.

If you insert the following in an ancestor component of a TresCanvas, it shouldn't shadow the TresCanvas's own provide('useTres', ...):

provide('useTres', 'I should not break anything')

But it does shadow it and replaces the context.

I think this should be:

provides = Object.assign({}, provides, currentInstance.provides)

Or without the assign:

      const provided = new Set(['useTres', 'extends'])
      [...]
          for (const key of Object.keys(currentInstance.provides)) {
            if (!provided.has(key)) {
              provide(key, currentInstance.provides[key])
              provided.add(key)
            }
          }

Copy link
Contributor

@andretchen0 andretchen0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @alvarosabu ,

I grabbed the latest revision. provide from outside the <TresCanvas> doesn't seem to work on components within the <TresCanvas> – I tried adding an inject in SubComponentWithInject.vue.

I could be wrong though. I didn't find a demo that showed how this is meant to be used, so I wrote a quick one, but maybe it's not how this is intended to be used. (I only found a demo that's intended to show that providing useTres doesn't break things)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this file git added by mistake maybe?

@alvarosabu alvarosabu marked this pull request as draft September 12, 2024 09:17
@alvarosabu
Copy link
Member Author

I grabbed the latest revision. provide from outside the doesn't seem to work on components within the – I tried adding an inject in SubComponentWithInject.vue.

I could be wrong though. I didn't find a demo that showed how this is meant to be used, so I wrote a quick one, but maybe it's not how this is intended to be used. (I only found a demo that's intended to show that providing useTres doesn't break things)

Hi @andretchen0 yeah no worries I was just trying stuff, will set this PR to draft.

I did manage to make it work with useTres and such but I can't make it work with reactivity. The inject inside just get the value the first time and it doesn't get updated further (for example clicking the button outside)

I'm currently clueless how to make it work, I opened a help request here in vue discord https://discord.com/channels/325477692906536972/1283405486175031418

@andretchen0
Copy link
Contributor

andretchen0 commented Sep 12, 2024

I'm currently clueless how to make it work

If we want to be explicit about provides that are "bridged", a component like this works:

<!-- ProvideBridge.vue -->
<script setup lang="ts">
import { provide } from 'vue'

interface Props {
  keysValues: Record<string, any>
}
const props = withDefaults(defineProps<Props>(), {
  keysValues: () => ({})
})
for (const [key, value] of Object.entries(props.keysValues)) {
  provide(key, value)
}
</script>

<template>
  <TresGroup>
    <slot></slot>
  </TresGroup>
</template>
 <TresCanvas>
    <TresPerspectiveCamera />
    <OrbitControls />

    <ProvideBridge :keys-values="{ foo: inject('foo') }">
        <Box /> <!-- Box can inject "foo" in its script setup -->
     </ProvideBridge>
</TresCanvas>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Impossible to inject variable in canvas child component Unable to use custom provide inject
2 participants