Skip to content

Commit 86b8b12

Browse files
committed
refactor: replaced plain refs to template elements with useTemplateRef
1 parent 789b83a commit 86b8b12

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+118
-115
lines changed

apps/docs/src/stories/Components/Tooltip.story.vue

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@
9292
</template>
9393

9494
<script lang="ts" setup>
95-
import { reactive, ref, type Ref, onMounted } from 'vue';
95+
import { reactive, ref, type Ref, onMounted, useTemplateRef } from 'vue';
9696
import { PfButton } from '@vue-patternfly/core';
9797
import type { Placement } from '@vue-patternfly/core/helpers/FloatingUi.vue';
9898
99-
const buttonRef: Ref<InstanceType<typeof PfButton> | undefined> = ref();
99+
const buttonRef = useTemplateRef('buttonRef');
100100
const trigger = reactive(['mouseenter', 'focus']);
101101
const leftAligned = ref(false);
102102
const flip = ref(true);
@@ -107,11 +107,13 @@ const exitDelay = ref(0);
107107
const distance = ref(15);
108108
109109
onMounted(() => {
110-
buttonRef.value?.el?.scrollIntoView({
111-
behavior: 'instant',
112-
block: 'center',
113-
inline: 'center',
114-
});
110+
if (buttonRef.value?.el instanceof HTMLElement) {
111+
buttonRef.value.el.scrollIntoView({
112+
behavior: 'instant',
113+
block: 'center',
114+
inline: 'center',
115+
});
116+
}
115117
});
116118
117119
function checkTrigger(value: string, checked: boolean) {

packages/core/src/components/Alert/Alert.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ import PfCloseButton from '../CloseButton.vue';
7777
import PfAlertIcon, { AlertVariantIcons } from './AlertIcon.vue';
7878
import PfAngleRightIcon from '@vue-patternfly/icons/angle-right-icon';
7979
import { useOUIAProps, type OUIAProps } from '../../helpers/ouia';
80-
import { ref, watch, type Ref, type HTMLAttributes, onBeforeUnmount, onMounted, computed, type Component } from 'vue';
80+
import { ref, watch, type HTMLAttributes, onBeforeUnmount, onMounted, computed, type Component, useTemplateRef, type ComponentPublicInstance } from 'vue';
8181
import { useElementSize } from '@vueuse/core';
8282
import { useManagedProp } from '../../use';
8383
import type { Placement } from '../../helpers/FloatingUi.vue';
@@ -149,7 +149,7 @@ defineSlots<{
149149
'action-links'?: (props?: Record<never, never>) => any;
150150
}>();
151151
152-
const titleRef: Ref<HTMLElement | undefined> = ref();
152+
const titleRef = useTemplateRef<HTMLElement | ComponentPublicInstance>('titleRef');
153153
const { width, height } = useElementSize(titleRef);
154154
const tooltipVisible = ref(false);
155155
let timer: number | undefined = undefined;
@@ -158,15 +158,15 @@ const isMouseOver = ref(false);
158158
const timedOut = ref(false);
159159
const timedOutAnimation = ref(true);
160160
const containsFocus = ref(false);
161-
const el: Ref<HTMLDivElement | null> = ref(null);
161+
const el = useTemplateRef('el');
162162
163163
const managedExpanded = useManagedProp('expanded', false);
164164
const variantLabel = computed(() => `${props.variant.charAt(0).toUpperCase()}${props.variant.slice(1)} alert:`);
165165
const dismissed = computed(() => timedOut.value && timedOutAnimation.value && !isMouseOver.value && !containsFocus.value);
166166
167167
168168
watch(() => [width.value, height.value], () => {
169-
if (!titleRef.value || !props.truncateTitle) {
169+
if (!(titleRef.value instanceof HTMLElement) || !props.truncateTitle) {
170170
return false;
171171
}
172172
tooltipVisible.value = titleRef.value.offsetHeight < titleRef.value.scrollHeight;

packages/core/src/components/Button.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import PfSpinner from './Spinner.vue';
6161
import PassThrough from '../helpers/PassThrough.vue';
6262
import type { RouteLocationRaw, useLink } from 'vue-router';
6363
import { useOUIAProps, type OUIAProps } from '../helpers/ouia';
64-
import { type Component, type UnwrapRef, type Ref, ref, computed, type AnchorHTMLAttributes, type ButtonHTMLAttributes } from 'vue';
64+
import { type Component, type UnwrapRef, computed, type AnchorHTMLAttributes, type ButtonHTMLAttributes, useTemplateRef, type ComponentPublicInstance } from 'vue';
6565
import { isDefined } from '@vueuse/shared';
6666
6767
type RouterLinkContext = UnwrapRef<ReturnType<typeof useLink>>;
@@ -138,7 +138,7 @@ defineSlots<{
138138
badge?: (props?: Record<never, never>) => any;
139139
}>();
140140
141-
const el: Ref<HTMLElement | undefined> = ref();
141+
const el = useTemplateRef<HTMLElement | ComponentPublicInstance>('el');
142142
const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe, variant: props.variant});
143143
144144
const buttonComponent = computed(() => {

packages/core/src/components/Checkbox.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
<script lang="ts" setup>
4848
import styles from '@patternfly/react-styles/css/components/Check/check';
49-
import { computed, ref, watch, type Component, type Ref, type InputHTMLAttributes, getCurrentInstance } from 'vue';
49+
import { computed, watch, type Component, type InputHTMLAttributes, getCurrentInstance, useTemplateRef } from 'vue';
5050
import { getUniqueId } from '../util';
5151
import { useOUIAProps, type OUIAProps } from '../helpers/ouia';
5252
import Sort from '../helpers/Sort.vue';
@@ -99,7 +99,7 @@ defineSlots<{
9999
}>();
100100
101101
useChildrenTracker(FormInputsKey, getCurrentInstance()?.proxy);
102-
const input: Ref<HTMLInputElement | undefined> = ref();
102+
const input = useTemplateRef('input');
103103
const wrapWithLabel = computed(() => (props.labelWrapped && !props.component) || props.component === 'label');
104104
const validId = computed(() => props.id || getUniqueId());
105105

packages/core/src/components/ChipGroup/Chip.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import PfButton from '../Button.vue';
6565
import PfTooltip from '../Tooltip/Tooltip.vue';
6666
import { getUniqueId } from '../../util';
6767
import { useElementOverflow } from '../../use';
68-
import { type Component, computed, ref, type Ref, type HTMLAttributes } from 'vue';
68+
import { type Component, computed, type HTMLAttributes, useTemplateRef } from 'vue';
6969
import type { Placement } from '../../helpers/FloatingUi.vue';
7070
7171
defineOptions({
@@ -100,7 +100,7 @@ defineSlots<{
100100
badge?: (props?: Record<never, never>) => any;
101101
}>();
102102
103-
const textRef: Ref<HTMLSpanElement | undefined> = ref();
103+
const textRef = useTemplateRef('textRef');
104104
const textOverflowing = useElementOverflow(textRef);
105105
const effectiveId = computed(() => props.id ?? getUniqueId());
106106
</script>

packages/core/src/components/ChipGroup/ChipGroup.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</template>
3838

3939
<script lang="ts" setup>
40-
import { h, ref, type Ref, type HTMLAttributes } from 'vue';
40+
import { h, ref, type HTMLAttributes, useTemplateRef } from 'vue';
4141
import styles from '@patternfly/react-styles/css/components/Chip/chip-group';
4242
import CircleXmarkIcon from '@vue-patternfly/icons/circle-xmark-icon';
4343
import PfChip from './Chip.vue';
@@ -84,7 +84,7 @@ const slots = defineSlots<{
8484
default?: (props?: Record<never, never>) => any;
8585
}>();
8686
87-
const labelRef: Ref<HTMLSpanElement | undefined> = ref();
87+
const labelRef = useTemplateRef('labelRef');
8888
const labelOverflowing = useElementOverflow(labelRef);
8989
const open = ref(props.defaultOpen);
9090

packages/core/src/components/DescriptionList/DescriptionListTermHelpTextButton.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<span
3-
ref="helpText"
3+
ref="helpTextRef"
44
v-bind="(ouiaProps as any)"
55
:class="[styles.descriptionListText, styles.modifiers.helpText]"
66
role="button"
@@ -13,7 +13,7 @@
1313

1414
<script lang="ts" setup>
1515
import styles from '@patternfly/react-styles/css/components/DescriptionList/description-list';
16-
import { ref, type HTMLAttributes, type Ref } from 'vue';
16+
import { useTemplateRef, type HTMLAttributes } from 'vue';
1717
import { useOUIAProps, type OUIAProps } from '../../helpers/ouia';
1818
1919
defineOptions({
@@ -30,7 +30,7 @@ defineSlots<{
3030
default?: (props?: Record<never, never>) => any;
3131
}>();
3232
33-
const helpTextRef: Ref<HTMLElement | undefined> = ref();
33+
const helpTextRef = useTemplateRef('helpTextRef');
3434
3535
function handleKeys(event: KeyboardEvent) {
3636
if (!helpTextRef.value || helpTextRef.value !== (event.target as HTMLElement)) {

packages/core/src/components/Drawer/Drawer.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<script lang="ts" setup>
2121
import styles from '@patternfly/react-styles/css/components/Drawer/drawer';
22-
import { computed, provide, type Ref, ref, type HTMLAttributes, watch } from 'vue';
22+
import { computed, provide, ref, type HTMLAttributes, watch, useTemplateRef } from 'vue';
2323
import AutoWrap from '../../helpers/AutoWrap.vue';
2424
import { DrawerKey } from './common';
2525
import PfDrawerContent from './DrawerContent.vue';
@@ -50,7 +50,7 @@ defineSlots<{
5050
default?: (props?: Record<never, never>) => any;
5151
}>();
5252
53-
const el: Ref<HTMLDivElement | undefined> = ref();
53+
const el = useTemplateRef('el');
5454
const display = ref(props.static || props.expanded);
5555
const expandedClass = ref(props.expanded);
5656
const isOpen = computed(() => props.static || props.expanded);

packages/core/src/components/Drawer/DrawerContent.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</template>
1919

2020
<script lang="ts">
21-
export type DrawerContentRef = Ref<HTMLDivElement | InstanceType<typeof AutoWrap> | undefined>;
21+
export type DrawerContentRef = Readonly<Ref<HTMLDivElement | InstanceType<typeof AutoWrap> | null>>;
2222
export const DrawerContentRefKey = Symbol('DrawerContentRefKey') as InjectionKey<DrawerContentRef>;
2323
2424
export interface Props extends /* @vue-ignore */ ComponentProps<typeof PfDrawerMain> {
@@ -29,7 +29,7 @@ export interface Props extends /* @vue-ignore */ ComponentProps<typeof PfDrawerM
2929

3030
<script lang="ts" setup>
3131
import styles from '@patternfly/react-styles/css/components/Drawer/drawer';
32-
import { type InjectionKey, provide, type Ref, ref } from 'vue';
32+
import { type InjectionKey, provide, type Ref, useTemplateRef } from 'vue';
3333
import { DrawerColorVariant } from './common';
3434
import type { ComponentProps } from '../../util';
3535
import AutoWrap from '../../helpers/AutoWrap.vue';
@@ -51,6 +51,6 @@ defineSlots<{
5151
content?: (props?: Record<never, never>) => any;
5252
}>();
5353
54-
const el: Ref<HTMLDivElement | undefined> = ref();
54+
const el = useTemplateRef('el');
5555
provide(DrawerContentRefKey, el);
5656
</script>

packages/core/src/components/Drawer/DrawerPanelContent.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import styles from '@patternfly/react-styles/css/components/Drawer/drawer';
4949
import cssPanelMdFlexBasis from '@patternfly/react-tokens/dist/esm/c_drawer__panel_md_FlexBasis';
5050
import cssPanelMdFlexBasisMin from '@patternfly/react-tokens/dist/esm/c_drawer__panel_md_FlexBasis_min';
5151
import cssPanelMdFlexBasisMax from '@patternfly/react-tokens/dist/esm/c_drawer__panel_md_FlexBasis_max';
52-
import { computed, inject, type Ref, ref, type HTMLAttributes } from 'vue';
52+
import { computed, inject, type Ref, ref, type HTMLAttributes, useTemplateRef } from 'vue';
5353
import { DrawerContentRefKey } from './DrawerContent.vue';
5454
import { getUniqueId } from '../../util';
5555
import { DrawerColorVariant, DrawerKey } from './common';
@@ -104,7 +104,7 @@ defineSlots<{
104104
default?: (props?: Record<never, never>) => any;
105105
}>();
106106
107-
const panel: Ref<HTMLElement | undefined> = ref();
107+
const panel = useTemplateRef('panel');
108108
const panelId = computed(() => props.id || getUniqueId('pf-drawer-panel-'));
109109
110110
const drawerProvide = inject(DrawerKey);
@@ -115,7 +115,7 @@ if (!drawerProvide) {
115115
116116
const { el: drawerRef, position, expanded, display, inline } = drawerProvide;
117117
const drawerContentRef = inject(DrawerContentRefKey);
118-
const splitterRef: Ref<HTMLDivElement | undefined> = ref();
118+
const splitterRef = useTemplateRef('splitterRef');
119119
120120
let isResizing: boolean | null = null;
121121
const panelSize: Ref<number | undefined> = ref();

0 commit comments

Comments
 (0)