Vue components¶
This guide describes how Vue components in app/frontend/src/ declare their API, split ownership of state between parent and child, expose test hooks, and translate texts. Type placement and naming of shared contracts are covered in Frontend TypeScript guidelines.
Using script setup with TypeScript¶
Write every component with <script setup lang="ts"> and the Composition API. Do not add new Options API components.
Single responsibility principle (SRP)¶
Give each component one clear purpose.
- Extract a child component when a template section has its own inputs, events, or repeated markup.
- Move Vue-dependent logic into composables and pure transformations into utils (see Composables and Utils).
- Compose DS components inside the feature module; create a new DS component only when one is explicitly requested.
Type the component API¶
Declare the public API with compiler macros and types, never with runtime-only option objects:
- Props:
defineProps<Props>(), withwithDefaultsfor optional props that need a default. Treat props as read-only. - Emits:
defineEmits<Emits>()with call signatures, one per event and payload. - Two-way value:
defineModel<T>()when the component owns the edited value and the parent binds it withv-model. Use a named model (defineModel<boolean>("expanded")) for a second bindable value. - Exposed API:
defineExposeonly for imperative actions a parent cannot express through props, such asfocusandblur. Do not expose internal state.
Keep a contract used by one component inside that component. Move it to the module's types/ folder when other files reuse it (see Keep types with their owner).
const modelValue = defineModel<string>({ default: "" });
const inputRef = ref<HTMLInputElement>();
defineExpose({
focus: () => inputRef.value?.focus(),
blur: () => inputRef.value?.blur(),
});
State management¶
Make ownership of each piece of state explicit:
- Keep state used by one component local (
ref,computed). - Use props and emits for a parent-child contract. The child reports what happened (
remove,update:value); the parent decides how to react, for example by calling a store action. - Name an event after the child's interaction, not after the parent's reaction: a row emits
remove, notdelete-bookmark-from-store. - Use a store for state that several components of a feature genuinely share. A presentational child does not need to read the store only because its parent does.
- Do not mutate props or copy them into local refs with a watcher. Emit a change or use
defineModel. - Treat a new watcher as a sign to recheck the design. Most have a simpler replacement, listed in Reach for a watcher last.
A typical split: a container component reads the list from the store and passes each item to a row component, which only receives props and emits events (Example component). See State management for store design.
Descriptive component naming¶
Name components after their purpose: BookmarkRow, BookmarkList, BookmarkActionsCell. Avoid generic names such as Item or Widget. Reserve the DS prefix for Design System components.
Test attributes (data-cy)¶
Add a data-cy attribute to interactive elements and to elements whose content a test asserts.
- Use kebab case scoped to the owning module:
name-of-module__name-of-element, with--unique-idonly when several instances need distinguishing. - Put the attribute on the DS component in the feature template; tests query it through the shared selector helpers described in Testing.
- Prefer
data-cyover DS classes or visible text when tests select elements.
Localization¶
Each layer translates its own texts:
| Text owner | Source | Rule |
|---|---|---|
| Feature module | window.EasyLocale.getLocale (usually const t = window.EasyLocale.getLocale) |
List the module's keys in constants/localeKeys.ts and load them with window.EasyLocale.fetchLocales before rendering. |
| Design System component | window.I18n.designSystem.<component> |
Rendered by easy_engines/easy_extensions/app/views/layouts/_base_html_header.html.erb and typed in shared/types/global.d.ts. DS components never call EasyLocale. |
| Server | Already translated values, such as a custom field name from GraphQL or a label passed to a ds_* helper |
Render the value as received. Do not translate it again or build locale keys from it. |
Feature code passes its translated texts to DS components through props such as label. Accessible names need translation too (see Name every control).
Example component¶
A presentational row that composes DS components, receives its data through typed props, and reports changes through typed emits. bookmarks is a made-up module: the example shows the pattern, not existing code.
<template>
<DSFieldset
with-border
removable
data-cy="bookmarks__row"
@remove="emit('remove')"
>
<DSTextField
:model-value="props.bookmark.title"
name="bookmarkTitle"
data-cy="bookmarks__row-title"
:label="t('bookmarks.field_title')"
required
@update:model-value="onTitleChange"
/>
</DSFieldset>
</template>
<script setup lang="ts">
import DSFieldset from "@/src/design_system/form_elements/fieldset/components/DSFieldset.vue";
import DSTextField from "@/src/design_system/form_elements/text_field/components/DSTextField.vue";
import type { DSTextFieldModelValue } from "@/src/design_system/form_elements/text_field/types/dsTextFieldTypes";
import type { Bookmark } from "@/src/bookmarks/types";
const props = defineProps<{
bookmark: Bookmark;
}>();
const emit = defineEmits<{
(e: "remove"): void;
(e: "update:title", title: string): void;
}>();
const t = window.EasyLocale.getLocale;
const onTitleChange = (title: DSTextFieldModelValue) => {
emit("update:title", title ?? "");
};
</script>
The parent owns the list and connects the events to store actions:
<BookmarkRow
:bookmark="bookmark"
@remove="removeBookmark(bookmark.id)"
@update:title="updateBookmarkTitle(bookmark.id, $event)"
/>
The row does not import the store, so it renders and can be tested with plain props.