Vue: the credits line and its Data attribution list are a single-file component

mountCreditsView() keeps its signature and stays the owner: it computes the
credits shown once each, keeps the list's open state and its focus, takes the
expand and close clicks and Escape, and renders CreditsView synchronously
(as the weather readout is rendered), so focus moves as soon as a change
returns. The view teleports the line and the list to the end of the body, as
the script appended them, so the page's rules that select them as children
of the body still apply; CreditItem draws one credit (its logo or its text,
only its linked words linked, links and logos kept only for a safe scheme).

The ids, classes, roles, aria attributes and text are unchanged. The list's
close button is a UiCloseButton with bare, keeping the browser's padding, so
it only gains the kit's classes, which carry no rules. The expand button
stays a plain button: its words are its content, which a UiButton slot would
not word again when the owner renders a new locale. The list's items are
keyed by credit, so a link that keeps its place keeps focus through a
redraw; one that goes still hands focus to the close button.

New tests: the line and list follow the locale, and they are the last of the
body with the kit's close button.

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-19 01:58:47 -08:00
1 parent 1f34210f71
commit 0b06bc6645
7 files changed
+267 -126

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
<script setup>
import { computed } from 'vue';
import {
LINK_SCHEMES,
LOGO_SCHEMES,
safeUrl,
splitLink,
} from '../../render/creditMarkup.js';
// One credit: its logo or its text, linked when it has a link. A credit is
// plain data (`src/render/contract/credits.d.ts`): its text is set as text,
// and a link or logo is kept only for a safe scheme.
const props = defineProps({
credit: { type: Object, required: true },
});
const href = computed(() =>
props.credit.href ? safeUrl(props.credit.href, LINK_SCHEMES) : null,
);
// A logo is drawn in place of the text, which names it (its alt text).
const logo = computed(() =>
props.credit.logoUrl ? safeUrl(props.credit.logoUrl, LOGO_SCHEMES) : null,
);
// Only the credit's linked words are the link, as the source wrote it.
const parts = computed(() =>
href.value && !logo.value && props.credit.linkText
? splitLink(props.credit)
: null,
);
</script>
<template>
<span v-if="parts" class="gev-credit"
>{{ parts[0]
}}<a :href="href" target="_blank" rel="noopener noreferrer">{{
parts[1]
}}</a
>{{ parts[2] }}</span
><component
:is="href ? 'a' : 'span'"
v-else
class="gev-credit"
:href="href || undefined"
:target="href ? '_blank' : undefined"
:rel="href ? 'noopener noreferrer' : undefined"
><img
v-if="logo"
class="gev-credit-logo"
:src="logo"
:alt="credit.text"
/><template v-else>{{ credit.text }}</template></component
>
</template>
+82
View File
@@ -0,0 +1,82 @@
<script setup>
import { t } from '../../i18n/index.js';
import UiCloseButton from '../kit/UiCloseButton.vue';
import CreditItem from './CreditItem.vue';
// The attribution line at the bottom left and the "Data attribution" list it
// opens, both at the end of `to` (the page's body), where the page's rules
// place them. The owner (src/ui/creditsView.js) computes the credits shown,
// keeps the list's open state and its focus, and takes the clicks and keys;
// `elements` receives the line's, the button's and the list's elements for
// it.
const props = defineProps({
to: { type: Object, required: true },
panelId: { type: String, required: true },
/** The credits on the line, once each. */
inline: { type: Array, required: true },
/** The credits in the list, once each. */
listed: { type: Array, required: true },
open: { type: Boolean, default: false },
/**
* The locale its words are in: a change words them again. The template
* never reads it; `t()` is not reactive, so a new value is what makes the
* view render again in the new language.
*/
locale: { type: String, default: '' },
elements: { type: Object, required: true },
onExpand: { type: Function, default: () => {} },
onClose: { type: Function, default: () => {} },
onKeydown: { type: Function, default: () => {} },
});
/** Between the line's credits, and before the list's button. */
const SEPARATOR = ' · ';
/** A credit's key: the same text and link are one credit. */
const key = (credit) => `${credit.text}\u0000${credit.href ?? ''}`;
/** Keep `name`'s element for the owner. */
const keep = (name) => (element) => {
props.elements[name] = element?.$el ?? element;
};
</script>
<template>
<Teleport :to="props.to"
><div id="gev-credits" :ref="keep('bar')" class="gev-credits" tabindex="-1"
><span :ref="keep('line')" class="gev-credits-line"
><template v-for="(credit, i) in props.inline" :key="key(credit)"
>{{ i ? SEPARATOR : '' }}<CreditItem :credit="credit" /></template
>{{ props.inline.length && props.listed.length ? SEPARATOR : '' }}</span
><button
:ref="keep('expand')"
type="button"
class="gev-credits-expand"
aria-haspopup="dialog"
:aria-controls="props.panelId"
:aria-expanded="String(props.open)"
:hidden="props.listed.length === 0"
@click="props.onExpand()"
>{{ t('Data attribution') }}</button
></div
><div
:id="props.panelId"
:ref="keep('panel')"
class="gev-credits-panel"
role="dialog"
:aria-labelledby="`${props.panelId}-title`"
:hidden="!props.open"
@keydown="props.onKeydown($event)"
><div class="gev-credits-header"
><span class="gev-credits-title" :id="`${props.panelId}-title`">{{
t('Data provided by:')
}}</span
><UiCloseButton
:ref="keep('close')"
class="gev-credits-close"
bare
:label="t('Close data attribution')"
@click="props.onClose()" /></div
><ul class="gev-credits-list"
><li v-for="credit in props.listed" :key="key(credit)"
><CreditItem :credit="credit" /></li></ul></div
></Teleport>
</template>