Patterns
This page explains which common patterns we follow when developing onyx and how to use them. These patterns are implemented through composables and enforced through linting rules, where possible.
Root Attribute Forwarding
For implementing necessary layout, styling and ARIA requirements, it is often necessary to wrap interactive HTML elements. To enable the developers to be able to set custom attributes and event-listeners on these, we forward most attributes to the relevant (e.g. input or button) element. The only attributes that are not forwarded are style
and class
with the assumption being, that these are only useful and intended to be set on the root element of the component.
/**
* Extension of `useAttrs` which splits root attributes from other properties.
* As root properties are considered: `style` and `class`.
*
* Make sure to call `defineOptions({ inheritAttrs: false });`.
*
* @example
* ```vue
* <script setup>
* defineOptions({ inheritAttrs: false });
* const { rootAttrs, restAttrs } = useRootAttrs();
* </script>
* <template>
* <div class="onyx-component" v-bind="rootAttrs">
* <!-- ... -->
* <input
* // some other attributes...
* v-bind="restAttrs"
* />
* <!-- ... -->
* </div>
* </template>
*/
export const useRootAttrs = <T extends HTMLAttributes = HTMLAttributes>() => {
INFO
Your use-case is not covered? Head over to our GitHub discussion page to make suggestions or ask questions!