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(NcAppContent): Allow to set the page title #5269

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 90 additions & 4 deletions src/components/NcAppContent/NcAppContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,45 @@ The list size must be between the min and the max width value.
:list-max-width="45"
>...</NcAppContent>
```

#### Usage: Custom document title
For accessibility reasons every document should have a `h1` heading,
this is visually hidden, but required for a semantically correct document.
You can use your app name or current view for the heading.

Additionally you can set a custom document title, e.g. to show the current status.

```vue
<template>
<NcAppContent :pageHeading="heading ? 'Heading' : undefined" :pageTitle="title ? 'Title' : undefined" >
<NcCheckboxRadioSwitch type="switch" :checked.sync="title">
Toggle title
</NcCheckboxRadioSwitch>
<NcCheckboxRadioSwitch type="switch" :checked.sync="heading">
Toggle Heading
</NcCheckboxRadioSwitch>
<NcButton @click="reset">Reset</NcButton>
</NcAppContent>
</template>

<script>
export default {
data() {
return {
heading: false,
title: false,
}
},
methods: {
reset() {
this.heading = false
this.title = false
document.title = ''
},
},
}
</script>
```
</docs>

<template>
Expand Down Expand Up @@ -120,17 +159,21 @@ The list size must be between the min and the max width value.
</template>

<script>
import NcAppDetailsToggle from './NcAppDetailsToggle.vue'
import { useIsMobile } from '../../composables/useIsMobile/index.js'

import { getBuilder } from '@nextcloud/browser-storage'
import { emit } from '@nextcloud/event-bus'
import { loadState } from '@nextcloud/initial-state'
import { useSwipe } from '@vueuse/core'
import { Splitpanes, Pane } from 'splitpanes'
import { useIsMobile } from '../../composables/useIsMobile/index.js'

import NcAppDetailsToggle from './NcAppDetailsToggle.vue'

import 'splitpanes/dist/splitpanes.css'
import { emit } from '@nextcloud/event-bus'

const browserStorage = getBuilder('nextcloud').persist().build()
const { name: productName } = loadState('theming', 'data', { name: 'Nextcloud' })
const activeApp = loadState('core', 'active-app', appName)
const localizedAppName = loadState('core', 'apps', {})[activeApp]?.name ?? appName

/**
* App content container to be used for the main content of your app
Expand Down Expand Up @@ -223,6 +266,18 @@ export default {
return ['no-split', 'vertical-split', 'horizontal-split'].includes(value)
},
},

/**
* Allow setting the page's `<title>`
*
* If a page heading is set it defaults to `{pageHeading} - {appName} - {productName}` e.g. `Favorites - Files - Nextcloud`.
* When the page heading and the app name is the same only one is used, e.g. `Files - Files - Nextcloud` is shown as `Files - Nextcloud`.
* When setting the prop then the following format will be used: `{pageTitle} - {pageHeading || appName} - {productName}`
*/
pageTitle: {
type: String,
default: null,
},
},

emits: [
Expand Down Expand Up @@ -289,6 +344,37 @@ export default {
},
}
},

realPageTitle() {
const entries = new Set()
if (this.pageTitle) {
entries.add(this.pageTitle)
}
if (this.pageHeading) {
entries.add(this.pageHeading)
}
if (entries.size === 0) {
return null
}

if (entries.size < 2) {
// Only add if not already pageHeading and pageTitle are included
entries.add(localizedAppName)
}
entries.add(productName)
return [...entries.values()].join(' - ')
},
},

watch: {
realPageTitle: {
immediate: true,
handler() {
if (this.realPageTitle !== null) {
document.title = this.realPageTitle
}
},
},
},

updated() {
Expand Down
82 changes: 82 additions & 0 deletions tests/unit/components/NcAppContent/NcAppContent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, expect, it } from '@jest/globals'
import { mount } from '@vue/test-utils'
import NcAppContent from '../../../../src/components/NcAppContent/NcAppContent.vue'

describe('NcAppContent', () => {
beforeEach(() => {
document.title = 'Initial title'
})

it('does not set a page heading by default', () => {
const wrapper = mount(NcAppContent)

expect(wrapper.find('h1').exists()).toBe(false)
})

it('can set the page heading', () => {
const wrapper = mount(NcAppContent, {
propsData: {
pageHeading: 'My heading',
},
})

expect(wrapper.find('h1').text()).toBe('My heading')
})

it('does not set the document title without page heading', () => {
mount(NcAppContent)

expect(document.title).toBe('Initial title')
})

it('sets the document title if a heading is provided', () => {
mount(NcAppContent, {
propsData: {
pageHeading: 'My heading',
},
})

expect(document.title).toBe('My heading - nextcloud-vue - Nextcloud')
})

it('does not duplicate the heading in the document title', () => {
mount(NcAppContent, {
propsData: {
pageHeading: 'nextcloud-vue',
},
})

expect(document.title).toBe('nextcloud-vue - Nextcloud')
})

it('sets the document title if pageTitle is provided', () => {
mount(NcAppContent, {
propsData: {
pageTitle: 'My title',
},
})

expect(document.title).toBe('My title - nextcloud-vue - Nextcloud')
})

it('does not duplicate the title in the document title', () => {
mount(NcAppContent, {
propsData: {
pageTitle: 'nextcloud-vue',
},
})

expect(document.title).toBe('nextcloud-vue - Nextcloud')
})

it('sets the document title if pageTitle and pageHeading are provided', () => {
mount(NcAppContent, {
propsData: {
pageHeading: 'My heading',
pageTitle: 'My title',
},
})

expect(document.title).toBe('My title - My heading - Nextcloud')
})
})
Loading