-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(files): Provide
useFileListWidth
composable
Replace the mixin with a composable, this is better typed and works in both: Options- and Composition API. Also added component tests for it. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
- Loading branch information
Showing
8 changed files
with
145 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*! | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { defineComponent } from 'vue' | ||
import { useFileListWidth } from './useFileListWidth.ts' | ||
|
||
const ComponentMock = defineComponent({ | ||
template: '<div id="test-component" style="width: 100%;background: white;">{{ fileListWidth }}</div>', | ||
setup() { | ||
return { | ||
fileListWidth: useFileListWidth(), | ||
} | ||
}, | ||
}) | ||
const FileListMock = defineComponent({ | ||
template: '<main id="app-content-vue" style="width: 100%;"><component-mock /></main>', | ||
components: { | ||
ComponentMock, | ||
}, | ||
}) | ||
|
||
describe('composable: fileListWidth', () => { | ||
|
||
it('Has initial value', () => { | ||
cy.viewport(600, 400) | ||
|
||
cy.mount(FileListMock, {}) | ||
cy.get('#app-content-vue') | ||
.should('be.visible') | ||
.and('contain.text', '600') | ||
}) | ||
|
||
it('Is reactive to size change', () => { | ||
cy.viewport(600, 400) | ||
cy.mount(FileListMock) | ||
cy.get('#app-content-vue').should('contain.text', '600') | ||
|
||
cy.viewport(800, 400) | ||
cy.screenshot() | ||
cy.get('#app-content-vue').should('contain.text', '800') | ||
}) | ||
|
||
it('Is reactive to style changes', () => { | ||
cy.viewport(600, 400) | ||
cy.mount(FileListMock) | ||
cy.get('#app-content-vue') | ||
.should('be.visible') | ||
.and('contain.text', '600') | ||
.invoke('attr', 'style', 'width: 100px') | ||
|
||
cy.get('#app-content-vue') | ||
.should('contain.text', '100') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*! | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import type { Ref } from 'vue' | ||
import { onMounted, readonly, ref } from 'vue' | ||
|
||
/** The element we observe */ | ||
let element: HTMLElement | undefined | ||
|
||
/** The current width of the element */ | ||
const width = ref(0) | ||
|
||
const observer = new ResizeObserver((elements) => { | ||
if (elements[0].contentBoxSize) { | ||
// use the newer `contentBoxSize` property if available | ||
width.value = elements[0].contentBoxSize[0].inlineSize | ||
} else { | ||
// fall back to `contentRect` | ||
width.value = elements[0].contentRect.width | ||
} | ||
}) | ||
|
||
/** | ||
* Update the observed element if needed and reconfigure the observer | ||
*/ | ||
function updateObserver() { | ||
const el = document.querySelector<HTMLElement>('#app-content-vue') ?? document.body | ||
if (el !== element) { | ||
// if already observing: stop observing the old element | ||
if (element) { | ||
observer.unobserve(element) | ||
} | ||
// observe the new element if needed | ||
observer.observe(el) | ||
element = el | ||
} | ||
} | ||
|
||
/** | ||
* Get the reactive width of the file list | ||
*/ | ||
export function useFileListWidth(): Readonly<Ref<number>> { | ||
// Update the observer when the component is mounted (e.g. because this is the files app) | ||
onMounted(updateObserver) | ||
// Update the observer also in setup context, so we already have an initial value | ||
updateObserver() | ||
|
||
return readonly(width) | ||
} |
Oops, something went wrong.