To achieve the desired layout where the trailing element is positioned absolutely relative to the input, you can wrap both the input and the trailing element in a container div. This container will serve as the positioning context for the absolutely positioned trailing element. Here's how you can modify your code:
<div class="relative">
<input
{...rest}
class={[
'stp:h-11 stp:sm:h-9',
'stp:rounded-md stp:px-3 stp:py-2 stp:text-sm stp:bg-white',
'stp:shadow-border stp:focus:z-10 stp:focus:shadow-border-focus stp:invalid:shadow-border-error stp:outline-none',
'stp:transition-[box-shadow_0.08s_ease-in,color_0.08s_ease-in,filter_50000s]',
`peer/input-${counter}`,
rest.class
]}
/>
{#if trailing}
<div class="stp:absolute stp:right-1 stp:top-1/2 stp:-translate-y-1/2">
{@render trailing()}
</div>
{/if}
</div>
Explanation:
- Container Div: The outer
div with the class relative establishes a positioning context for its absolutely positioned children. This ensures that the trailing element is positioned relative to this container.
- Input Element: The
input element remains the same, with all its classes and props.
- Trailing Element: The
div containing the trailing element is positioned absolutely with stp:absolute. The stp:right-1, stp:top-1/2, and stp:-translate-y-1/2 classes ensure that the trailing element is positioned correctly relative to the input.
This setup ensures that the trailing element is always positioned correctly relative to the input, regardless of the input's size or position within the layout.