1

I added the TreeView in __layout.svelte, and on:select I use svelte's goto() to jump to a path defined for the selected id.

But there are several issues:

  • If I pass the url of a sub page on the tree, the left tree can't be selected correctly, it always select the default one.
  • Similarly, If I click a link, the left tree can't be selected correctly.

I saw the left tree from document works well.
How is that done?

Questions:

  • So, how to change / init the activeId properly, when the id is not selected by click items in the tree?
  • Is there a better way to share the tree among multiple route pages?
Eric
  • 22,183
  • 20
  • 145
  • 196
  • Also asked in github issues: https://github.com/carbon-design-system/carbon-components-svelte/issues/1403 – Eric Jul 16 '22 at 01:45
  • Please do not create spam like that in the Github issues. – H.B. Jul 16 '22 at 01:57
  • @H.B. OK, I've closed the github issue with link to this question, I was not sure where to ask the question (github or SO). – Eric Jul 16 '22 at 01:59
  • 1
    If you want to know how the navigation of the docs is implemented, you can [read its code](https://github.com/carbon-design-system/carbon-components-svelte/blob/f8e400f2edd971f41dacfbc21bfff4ee059e5163/docs/src/pages/_layout.svelte#L100). – H.B. Jul 16 '22 at 02:00
  • @H.B. So, use `SideNav` instead of `TreeView` as described in https://stackoverflow.com/a/73000993, there won't be such issue any more ? Let me try. – Eric Jul 16 '22 at 02:01
  • Cool, let me rewrite my code with the link. – Eric Jul 16 '22 at 02:02
  • 1
    You still need to use it correctly, and the docs do not use SvelteKit and its router, but [`@sveltech/routify`](https://www.npmjs.com/package/@sveltech/routify). – H.B. Jul 16 '22 at 02:03
  • @H.B. Just replaced `` with ``, make the `` inside ``, works very well. – Eric Jul 16 '22 at 03:10
  • You can post that as an answer – H.B. Jul 16 '22 at 03:14
  • @H.B. Answer added. – Eric Jul 16 '22 at 03:23

1 Answers1

0

(As mentioned in comment by @H.B , using <SideNav> from UIShell instead of <TreeView> solved the problem.)

You may refer to this example from the document.

Here is part of my code in __layout.svelte:

<script>
  import "../../app.css";
  import "carbon-components-svelte/css/all.css";

  import {
    SideNav,
    SideNavItems,
    SideNavMenu,
    SideNavMenuItem,
    SideNavLink,
    SideNavDivider,
    Content,
    Grid,
    Row,
    Column,
  } from "carbon-components-svelte";

  let isSideNavOpen = true;
</script>

<SideNav bind:isOpen={isSideNavOpen}>
  <SideNavItems>
    <SideNavLink href="/home" text="Home"/>
    <SideNavLink href="/home/content" text="Contents"/>
    <SideNavMenu text="Menu">
      <SideNavMenuItem href="/home" text="Home 1"/>
      <SideNavMenuItem href="/home" text="Home 2"/>
    </SideNavMenu>
    <SideNavDivider/>
    <SideNavLink href="/" text="Index"/>
  </SideNavItems>
</SideNav>

<Content>
  <Grid>
    <Row>
      <Column>
        <slot></slot>
      </Column>
    </Row>
  </Grid>
</Content>

Tips:

  • Should add <slot> inside <Content>, otherwise the side nav might overlap part of the slot.

BTW, I'm using this in svelte-kit.

Eric
  • 22,183
  • 20
  • 145
  • 196
  • Note that the UI shell should include a `Header` which needs to bind `isSideNavOpen`: `
    `. On small screens, e.g. mobile phones, the `SideNav` will be hidden automatically and the header will show a button which opens is. You could of course implement your own button that fulfills this role on small screens.
    – H.B. Jul 16 '22 at 04:32
  • @H.B. Seems the `
    ` is for ``, and optional for ``, I'm not sure. But, if I wrap `` inside `
    `, the side bar overlap the content part. I'll check how to hide/show side nav later, for now I always show it (`let isSideNavOpen = true`).
    – Eric Jul 16 '22 at 04:40
  • BTW, I have another top level `__layout.svelte`, if I add `
    ` in the 2nd level `__layout.svelte`, seems it will also hide the non-slot part of the parent `__layout.svelte`.
    – Eric Jul 16 '22 at 04:43
  • `` is optional and only for the navigation at the top. `
    ` should appear before `` at the same level, see the [example in the documentation](https://carbon-components-svelte.onrender.com/components/UIShell#header-with-side-navigation).
    – H.B. Jul 16 '22 at 04:45
  • There seems to be some issue, I'll try more on this later, and update here if found more useful results. – Eric Jul 16 '22 at 04:54
  • After a bit research & try, I decided to use https://github.com/themesberg/flowbite-svelte , which contains a `sidebar` component https://flowbite-svelte.com/sidebars , it fits my needs well for now. It's pretty new, but pretty good from my experience so far. – Eric Jul 18 '22 at 03:00