@onsvisual/svelte-components 1.0.35 → 1.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,12 +33,15 @@
33
33
  const observer = getContext("observer");
34
34
  const tocId = getContext("tocId");
35
35
 
36
+ let section;
36
37
  let mounted = false;
37
38
  let observed = false;
38
39
 
39
- // This allows the table of contents (toc) to render before hydration
40
- let section = { id, dataset: { title, subsection: String(subsection) } };
41
- $sections = [...$sections.filter((s) => s.id !== section.id), section];
40
+ // This should allow the table of contents (toc) to render before hydration
41
+ $sections = [
42
+ ...$sections.filter((s) => s.id !== id),
43
+ { id, dataset: { title, subsection: String(subsection) } }
44
+ ];
42
45
 
43
46
  // This allows sections to be highlighted on the toc after hydration,
44
47
  // and for sections to be added/removed gracefully from the DOM
@@ -39,7 +39,9 @@
39
39
  */
40
40
  export let marginBottom = true;
41
41
 
42
+ let el;
42
43
  let active;
44
+ let mounted = false;
43
45
 
44
46
  setContext("tocId", id);
45
47
 
@@ -50,6 +52,7 @@
50
52
  setContext("observer", observer);
51
53
 
52
54
  function formatSections(sections) {
55
+ if (sections.length === 0) return [];
53
56
  const secs = [];
54
57
  let sec = { subsections: [] };
55
58
  for (const section of sections) {
@@ -62,7 +65,6 @@
62
65
  }
63
66
  return [...secs, sec];
64
67
  }
65
- $: formattedSections = formatSections($sections);
66
68
 
67
69
  onMount(() => {
68
70
  if (!noContents) {
@@ -82,12 +84,25 @@
82
84
  }
83
85
  );
84
86
  }
87
+ // The table of contents doesn't populate unless it's after the main content
88
+ // This is a trick to flip the order when the page hydrates
89
+ el.prepend(el.lastElementChild);
90
+ mounted = true;
85
91
  });
86
92
  </script>
87
93
 
88
94
  <Container {cls} {width} {marginTop} {marginBottom}>
89
- <div class="ons-grid ons-grid--spaced ons-grid--loose ons-js-toc-container">
90
- <div class="ons-grid__col ons-grid__col--sticky@m ons-col-4@m">
95
+ <div
96
+ class="ons-grid ons-grid--spaced ons-grid--loose ons-js-toc-container"
97
+ class:flip-display-order={!mounted}
98
+ bind:this={el}
99
+ >
100
+ <div class="ons-nav-sections-content ons-grid__col ons-col-8@m">
101
+ <slot name="before" />
102
+ <slot />
103
+ <slot name="after" />
104
+ </div>
105
+ <div class="ons-nav-sections-toc ons-grid__col ons-grid__col--sticky@m ons-col-4@m">
91
106
  <aside class="ons-table-of-contents-container" role="complementary">
92
107
  <slot name="before-nav" />
93
108
  {#if !noContents}
@@ -96,47 +111,50 @@
96
111
  <h2 class="ons-table-of-contents__title ons-u-fs-r--b ons-u-mb-s">Contents</h2>
97
112
  {/if}
98
113
  <ol class="ons-list ons-u-mb-l ons-list--dashed">
99
- {#if formattedSections}
100
- {#each formattedSections as section}
101
- <li class="ons-list__item">
102
- {#if section.id && section.title}
103
- <a
104
- href="#{section.id}"
105
- class="ons-list__link"
106
- class:ons-table-of-contents__link-active={section.id === active}
107
- >
108
- {section.title}
109
- </a>
110
- {/if}
111
- {#if section.subsections.length > 0}
112
- <ol class="ons-list ons-u-mb-no ons-list--dashed">
113
- {#each section.subsections as subsection}
114
- <li class="ons-list__item">
115
- <a
116
- href="#{subsection.id}"
117
- class="ons-list__link"
118
- class:ons-table-of-contents__link-active={subsection.id === active}
119
- >
120
- {subsection.title}
121
- </a>
122
- </li>
123
- {/each}
124
- </ol>
125
- {/if}
126
- </li>
127
- {/each}
128
- {/if}
114
+ {#each formatSections($sections) as section}
115
+ <li class="ons-list__item">
116
+ {#if section.id && section.title}
117
+ <a
118
+ href="#{section.id}"
119
+ class="ons-list__link"
120
+ class:ons-table-of-contents__link-active={section.id === active}
121
+ >
122
+ {section.title}
123
+ </a>
124
+ {/if}
125
+ {#if section.subsections.length > 0}
126
+ <ol class="ons-list ons-u-mb-no ons-list--dashed">
127
+ {#each section.subsections as subsection}
128
+ <li class="ons-list__item">
129
+ <a
130
+ href="#{subsection.id}"
131
+ class="ons-list__link"
132
+ class:ons-table-of-contents__link-active={subsection.id === active}
133
+ >
134
+ {subsection.title}
135
+ </a>
136
+ </li>
137
+ {/each}
138
+ </ol>
139
+ {/if}
140
+ </li>
141
+ {/each}
129
142
  </ol>
130
143
  </nav>
131
144
  {/if}
132
145
  <slot name="after-nav" />
133
146
  </aside>
134
147
  </div>
135
- <div class="ons-grid__col ons-col-8@m">
136
- <slot name="before" />
137
- <slot />
138
- <slot name="after" />
139
- </div>
140
148
  </div>
141
149
  <slot name="footer" />
142
150
  </Container>
151
+
152
+ <style>
153
+ /* These classes force the table of contents to render on the left of the page before hydration */
154
+ .flip-display-order {
155
+ direction: rtl;
156
+ }
157
+ .flip-display-order > * {
158
+ direction: initial;
159
+ }
160
+ </style>
@@ -12,11 +12,11 @@ export default class NavSections extends SvelteComponentTyped<{
12
12
  }, {
13
13
  [evt: string]: CustomEvent<any>;
14
14
  }, {
15
- 'before-nav': {};
16
- 'after-nav': {};
17
15
  before: {};
18
16
  default: {};
19
17
  after: {};
18
+ 'before-nav': {};
19
+ 'after-nav': {};
20
20
  footer: {};
21
21
  }> {
22
22
  }
@@ -38,11 +38,11 @@ declare const __propDef: {
38
38
  [evt: string]: CustomEvent<any>;
39
39
  };
40
40
  slots: {
41
- 'before-nav': {};
42
- 'after-nav': {};
43
41
  before: {};
44
42
  default: {};
45
43
  after: {};
44
+ 'before-nav': {};
45
+ 'after-nav': {};
46
46
  footer: {};
47
47
  };
48
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "1.0.35",
3
+ "version": "1.0.37",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "npm run build:package && npm run build:docs",