Giter Site home page Giter Site logo

Self-references/partials about svelte HOT 6 CLOSED

sveltejs avatar sveltejs commented on September 27, 2024 8
Self-references/partials

from svelte.

Comments (6)

mrkishi avatar mrkishi commented on September 27, 2024 1

Is a special construct really needed for this? It looks like we could use the component recursively as long as the compiler added a self-reference in its components property, somewhat akin to this:

const MyComponent = { components: {} };
MyComponent.components.MyComponent = realMyComponent;

export default MyComponent;
{{if recurse}}
    <MyComponent/>
{{/if}}

I think something along these lines should work, and it'd be closer to native Custom Elements behavior.

from svelte.

pantchox avatar pantchox commented on September 27, 2024

Does it mean that something like this:

<div 
    on:click='set({accordionOpen: !accordionOpen})' class="accordion-container">
    <div class="accordion-header">
        {{headerComp}}
    </div>
    {{#if accordionOpen}}
    <div class="accordion-content">
        {{contentComp}}
    </div>
    {{/if}}
</div>

making it work like this:

<div>
    <Accordion headerComp=HeaderComp contentComp=ContentComp />
</div>

HeaderComp and ContentComp are any other regular imported components with their own data() functions initialized already.

from svelte.

mindplay-dk avatar mindplay-dk commented on September 27, 2024

So, something like a tree-view would become possible by using two components, one for the tree node and one for the tree itself, similar to this example in Vue.

For something like a tree-view, being able to put both the node and root views/components in the same file makes a lot of sense though - in fact, keeping the node component itself as a private component of the tree-view itself would make a lot of sense.

What I would like is for the export default declaration to be used to export the tree-view component itself, while declaring the tree-node component separately, would that work? We would need some way to designate HTML and CSS sections as belonging to a specific named component, maybe with an attribute like svelte:component or a <template> tag to group the HTML and CSS tags together?

from svelte.

Rich-Harris avatar Rich-Harris commented on September 27, 2024

Thanks for the feedback everyone — finally got round to tackling this, have opened #332.

@mrkishi Unfortunately that wouldn't work as you can't export a reference, you have to export an object literal. That's so that Svelte can do static analysis on your component at compile time.

@mindplay-dk The root and the nodes would have to be two separate components, but that's ok since the node would only depend on itself

from svelte.

Rich-Harris avatar Rich-Harris commented on September 27, 2024

Released in 1.10.0. You can see it in action on Svelte Hacker News, by clicking on a comment thread (comments embed their children recursively)

from svelte.

mindplay-dk avatar mindplay-dk commented on September 27, 2024

Feels incredibly fast! Excited to test this out :-)

from svelte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.