Skip to content

Commit fbd7a69

Browse files
committed
docs: add sidebar tab example to README
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 4f483c4 commit fbd7a69

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,120 @@ const myEntry: Entry = {
5353
addNewFileMenuEntry(myEntry)
5454
```
5555

56+
#### Register a sidebar tab
57+
58+
It is possible to provide your own sidebar tabs for the files app.
59+
For this you need to create a [custom web component](https://developer.mozilla.org/en-US/docs/Web/API/Web_components),
60+
which can either be done without any framework by using vanilla JavaScript but is also [possible with Vue](https://vuejs.org/guide/extras/web-components#building-custom-elements-with-vue).
61+
62+
This example will make use of the Vue framework for building a sidebar tab as this is the official UI framework for Nextcloud apps.
63+
64+
The sidebar tab consists of two parts:
65+
1. The web component which will be rendered within the sidebar.
66+
2. A definition object that provides all information needed by the files app.
67+
68+
##### SidebarTab definition object
69+
70+
This object provides the requires information such as:
71+
- The order (to ensure a consistent tabs order)
72+
- The display name for the tab navigation
73+
- An icon, to be used in the tab navigation
74+
- A callback to check if the sidebar tab is enabled for the current node shown in the sidebar.
75+
- The web component tag name
76+
77+
The registration must happen in an `initScript`.
78+
79+
```ts
80+
import type { ISidebarTab } from '@nextcloud/files'
81+
82+
import { getSidebar } from '@nextcloud/files'
83+
import { t } from '@nextcloud/l10n'
84+
85+
const MyTab: ISidebarTab = {
86+
// unique ID of the tab
87+
id: 'my_app',
88+
// The display name in the menu
89+
displayName: t('my_app', 'Sharing'),
90+
// Pass an SVG (string) to be used as the tab menu entry icon
91+
iconSvgInline: '<svg>...</svg>',
92+
// Lower values mean a more prominent position
93+
order: 50,
94+
// the tag name of the web component
95+
tagName: 'my_app-files_sidebar_tab',
96+
enabled({ node, folder, view }) {
97+
// you can disable this tab for some cased based on:
98+
// - node: The node the sidebar was opened for
99+
// - folder: The folder currently shown in the files app
100+
// - view: The currently active files view
101+
return true
102+
},
103+
104+
// OPTIONAL
105+
async register() {
106+
// This is called when the tab is about to be activated the first time.
107+
// So this can be used to do some initialization or even to define the web component.
108+
}
109+
}
110+
111+
// the you need to register it in the sidebar
112+
getSidebar()
113+
.registerTab(MyTab)
114+
```
115+
116+
##### SidebarTab web component
117+
118+
The web component needs to have those properties:
119+
- node of type `INode`
120+
- folder of type `IFolder`
121+
- view of type `IView`
122+
123+
And should have at least one method:
124+
- `setActive(active: boolean)`
125+
126+
When using Vue you need to first create the Vue component:
127+
128+
```vue
129+
<script setup lang="ts">
130+
import type { IFolder, INode, IView } from '@nextcloud/files'
131+
132+
defineProps<{
133+
node: INode
134+
folder: IFolder
135+
view: IView
136+
}>()
137+
138+
defineExpose({ setActive })
139+
140+
function setActive(active: boolean) {
141+
console.debug('Tab was set active', { active })
142+
}
143+
</script>
144+
145+
<template>
146+
<div>
147+
<div>Showing node: {{ node.source }}</div>
148+
<div>... in folder: {{ folder.source }}</div>
149+
<div>... with view: {{ view.id }}</div>
150+
</div>
151+
</template>
152+
```
153+
154+
Which then can be wrapped in a web component and registered.
155+
156+
```ts
157+
import { defineAsyncComponent, defineCustomElement } from 'vue'
158+
159+
const tab: ISidebarTab = {
160+
// ...
161+
onInit() {
162+
const MySidebarTab = defineAsyncComponent(() => import('./views/MySidebarTab.vue'))
163+
// make sure to disable the shadow root to allow theming with Nextcloud provided global styles.
164+
const MySidebarTabWebComponent = defineCustomElement(MySidebarTab, { shadowRoot: false })
165+
customElements.define('my_app-files_sidebar_tab', MySidebarTabWebComponent)
166+
}
167+
}
168+
```
169+
56170
### WebDAV
57171
The `getClient` exported function returns a webDAV client that's a wrapper around [webdav's webDAV client](https://www.npmjs.com/package/webdav).
58172
All its methods are available here.

0 commit comments

Comments
 (0)