-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
useAreaData()
composable (refs SFKUI-6992)
- Loading branch information
Showing
6 changed files
with
133 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: useAreaData() composable | ||
short-title: useAreaData() | ||
name: useAreaData | ||
layout: article | ||
--- | ||
|
||
Composable för att hämta information om den {@link FPageLayout layout yta} ett element befinner sig i: | ||
|
||
- Ytans namn. | ||
- Om paneler ska anslutas till höger eller vänster. | ||
- Vilken riktning innehållet i ytan ska flöda. | ||
|
||
## Syntax | ||
|
||
```ts nocompile nolint | ||
function useAreaData(element); | ||
``` | ||
|
||
### Parametrar | ||
|
||
`element: HTMLElement` | ||
: Elementet förfrågning om yta ska göras för. | ||
|
||
### Returvärde | ||
|
||
`area: string` | ||
: Ytans namn. | ||
|
||
`attachPanel: "none" | "left" "right"` | ||
: Om paneler ska anslutas till höger eller vänster inom ytan. | ||
|
||
`direction: "column" | "row"` | ||
: Vilken riktning innehållet i ytan ska flöda. | ||
|
||
## Exempel | ||
|
||
Sätter en klass baserat på hur panelen ska fästas: | ||
|
||
```ts | ||
import { computed, useTemplateRef } from "vue"; | ||
import { useAreaData } from "@fkui/vue"; | ||
|
||
/* --- cut above -- */ | ||
|
||
const element = useTemplateRef("my-root-element"); | ||
const { attachPanel } = useAreaData(element); | ||
|
||
const attachClass = computed(() => { | ||
switch (attachPanel.value) { | ||
case "left": | ||
return "attach-left"; | ||
case "right": | ||
return "attach-right"; | ||
} | ||
return undefined; | ||
}); | ||
``` | ||
|
||
## Related | ||
|
||
- {@link FPageLayout} | ||
- {@link custom-page-layout Egen layout till applikationsmall} |
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 @@ | ||
import { type Ref, ref, type ShallowRef, watchEffect } from "vue"; | ||
import { LayoutAreaAttachPanel, LayoutAreaDirection } from "./define-layout"; | ||
import { | ||
VAR_NAME_AREA, | ||
VAR_NAME_ATTACH_PANEL, | ||
VAR_NAME_DIRECTION, | ||
} from "./constants"; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface UseAreaData { | ||
/** Name of layout area */ | ||
readonly area: Readonly<Ref<string | null>>; | ||
/** Panel attachment */ | ||
readonly attachPanel: Readonly<Ref<LayoutAreaAttachPanel | null>>; | ||
/** Direction area content flows */ | ||
readonly direction: Readonly<Ref<LayoutAreaDirection | null>>; | ||
} | ||
|
||
function getProperty<T>(style: CSSStyleDeclaration, key: string): T | null { | ||
const value = style.getPropertyValue(key); | ||
if (value === "") { | ||
return null; | ||
} else { | ||
return JSON.parse(value); | ||
} | ||
} | ||
|
||
/** | ||
* Fetch information about the layout area given element belongs to. | ||
* | ||
* @public | ||
*/ | ||
export function useAreaData( | ||
element: Readonly<ShallowRef<HTMLElement | null | undefined>>, | ||
): UseAreaData { | ||
const area = ref<string | null>(null); | ||
const attachPanel = ref<LayoutAreaAttachPanel | null>(null); | ||
const direction = ref<LayoutAreaDirection | null>(null); | ||
|
||
watchEffect(() => { | ||
if (element.value) { | ||
const style = getComputedStyle(element.value); | ||
area.value = getProperty(style, VAR_NAME_AREA); | ||
attachPanel.value = getProperty(style, VAR_NAME_ATTACH_PANEL); | ||
direction.value = getProperty(style, VAR_NAME_DIRECTION); | ||
} | ||
}); | ||
|
||
return { | ||
area, | ||
attachPanel, | ||
direction, | ||
}; | ||
} |
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