|
| 1 | +--- |
| 2 | +title: Addons |
| 3 | +sort: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +Addons provide a way to prepend or append text or an icon to an input. Both leading and trailing addons can be added to the input either by use of props or slots. |
| 9 | +When rendering the input, only one type of leading and one type of trailing addon will be rendered at a time. |
| 10 | + |
| 11 | +In the examples shown on this page, we will be using the `input` component, however addons will work with most other components as well. |
| 12 | + |
| 13 | +## Leading Addons |
| 14 | + |
| 15 | +There are three types of "leading" addons offered by this package: leading addon, inline addon, and leading icon. Each of these will be rendered somewhere |
| 16 | +on the left side of an input element. |
| 17 | + |
| 18 | +### Leading Addon |
| 19 | + |
| 20 | +A leading addon will be rendered as text on top of a light gray background at the beginning of the input. To render a leading |
| 21 | +addon, specify the text either in the `leading-addon` attribute or the `leadingAddon` slot. |
| 22 | + |
| 23 | +Via prop: |
| 24 | + |
| 25 | +```html |
| 26 | +<x-input name="url" leading-addon="https://" /> |
| 27 | +``` |
| 28 | + |
| 29 | +Via slot: |
| 30 | + |
| 31 | +```html |
| 32 | +<x-input name="url"> |
| 33 | + <x-slot:leading-addon>https://</x-slot:leading-addon> |
| 34 | +</x-input> |
| 35 | +``` |
| 36 | + |
| 37 | +### Inline Addon |
| 38 | + |
| 39 | +Inline addon is similar to leading addon, however there is no background behind the text. You are also responsible |
| 40 | +for setting the left padding of the input to allow for enough room for your text to fit at the beginning of the |
| 41 | +input. |
| 42 | + |
| 43 | +You can set the inline addon by using the `inline-addon` attribute or the `inlineAddon` slot. |
| 44 | + |
| 45 | +Via prop: |
| 46 | + |
| 47 | +```html |
| 48 | +<x-input name="url" inline-addon="https://" /> |
| 49 | +``` |
| 50 | + |
| 51 | +Via slot: |
| 52 | + |
| 53 | +```html |
| 54 | +<x-input name="url"> |
| 55 | + <x-slot:inline-addon>https://</x-slot:inline-addon> |
| 56 | +</x-input> |
| 57 | +``` |
| 58 | + |
| 59 | +To modify the padding, you should use a custom class on the input. Since this padding value is controlled via a [variable](/docs/laravel-form-components/{version}/advanced-usage/customizing-css#user-content-variables), |
| 60 | +you could just use an [arbitrary value](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values) in a custom class name: |
| 61 | + |
| 62 | +```html |
| 63 | +<x-input |
| 64 | + name="url" |
| 65 | + inline-addon="https://" |
| 66 | + class="![--inline-addon-pl:theme(spacing.4)]" |
| 67 | +/> |
| 68 | +``` |
| 69 | + |
| 70 | +This is the equivalent of `pl-4` on the input. However, it's recommended to override the variable value instead. If you do it this way, you need to be sure to make the rule |
| 71 | +`!important` (prefix the class name with `!`) so it actually overrides the variable value. |
| 72 | + |
| 73 | +### Leading Icon |
| 74 | + |
| 75 | +Instead of text, you can prepend an icon to the input instead. The package is styled for |
| 76 | +[blade heroicon svgs](https://github.com/blade-ui-kit/blade-heroicons), but you are free |
| 77 | +to use whatever icons you want to. |
| 78 | + |
| 79 | +To prepend an icon, you can use the `leading-icon` prop, or the `leadingIcon` slot: |
| 80 | + |
| 81 | +Via prop: |
| 82 | + |
| 83 | +```html |
| 84 | +<x-input name="url" leading-icon="heroicon-m-at-symbol" /> |
| 85 | +``` |
| 86 | + |
| 87 | +Via slot: |
| 88 | + |
| 89 | +```html |
| 90 | +<x-input name="url"> |
| 91 | + <x-slot:leading-icon> |
| 92 | + <x-heroicon-m-at-symbol /> |
| 93 | + </x-slot:leading-icon> |
| 94 | +</x-input> |
| 95 | +``` |
| 96 | + |
| 97 | +If you pass the icon name via prop, the component will use the `<x-dynamic-component />` component behind-the-scenes to render the component for convenience. |
| 98 | + |
| 99 | +## Trailing Addons |
| 100 | + |
| 101 | +There are three types of "trailing" addons offered by this package: trailing addon, trailing inline addon, and trailing icon. These all behave like the leading addon version of them, except now they |
| 102 | +will be on the right side of the input element. |
| 103 | + |
| 104 | +### Trailing Addon |
| 105 | + |
| 106 | +Like the leading addon, the trailing addon will render text inside a light gray background at the end of the input this time. To render a trailing addon, |
| 107 | +specify text in either the `trailing-addon` prop or the `trailingAddon` slot. |
| 108 | + |
| 109 | +Via prop: |
| 110 | + |
| 111 | +```html |
| 112 | +<x-input name="url" trailing-addon="lbs" /> |
| 113 | +``` |
| 114 | + |
| 115 | +Via slot: |
| 116 | + |
| 117 | +```html |
| 118 | +<x-input name="url"> |
| 119 | + <x-slot:trailing-addon>lbs</x-slot:trailing-addon> |
| 120 | +</x-input> |
| 121 | +``` |
| 122 | + |
| 123 | +### Trailing Inline Addon |
| 124 | + |
| 125 | +Like the inline addon, the trailing inline addon will add text directly inside the input, but this time on the right |
| 126 | +side of the input. Also like the inline addon, you may need to specify a custom padding amount for the input. |
| 127 | + |
| 128 | +You can add a trailing inline addon by using either the `trailing-inline-addon` attribute or the `trailingInlineAddon` slot. |
| 129 | + |
| 130 | +Via prop: |
| 131 | + |
| 132 | +```html |
| 133 | +<x-input name="amount" trailing-inline-addon="USD" /> |
| 134 | +``` |
| 135 | + |
| 136 | +Via slot: |
| 137 | + |
| 138 | +```html |
| 139 | +<x-input name="amount"> |
| 140 | + <x-slot:trailing-inline-addon>USD</x-slot:trailing-inline-addon> |
| 141 | +</x-input> |
| 142 | +``` |
| 143 | + |
| 144 | +To adjust the amount of padding on the right side on the input, you should override the `--inline-addon-pr` CSS variable. See [Inline Addon](#user-content-inline-addon) |
| 145 | +for an example on how to this. |
| 146 | + |
| 147 | +### Trailing Icon |
| 148 | + |
| 149 | +You can append an icon to an input similar to prepending one. You can do so using either the `trailing-icon` prop or the `trailingIcon` slot: |
| 150 | + |
| 151 | +Via prop: |
| 152 | + |
| 153 | +```html |
| 154 | +<x-input name="url" trailing-icon="heroicon-m-magnifying-glass" /> |
| 155 | +``` |
| 156 | + |
| 157 | +Via slot: |
| 158 | + |
| 159 | +```html |
| 160 | +<x-input name="url"> |
| 161 | + <x-slot:trailing-icon> |
| 162 | + <x-heroicon-m-magnifying-glass /> |
| 163 | + </x-slot:trailing-icon> |
| 164 | +</x-input> |
| 165 | +``` |
| 166 | + |
| 167 | +If you pass the icon name via prop, the component will use the `<x-dynamic-component />` component behind-the-scenes to render the component for convenience. |
| 168 | + |
| 169 | +## Before Slot |
| 170 | + |
| 171 | +The `before` slot allows you the flexibility to render any kind of HTML you need to directly before the input element. If you have a leading addon on the input, it will render before that markup. |
| 172 | + |
| 173 | +```html |
| 174 | +<x-input> |
| 175 | + <x-slot:before> |
| 176 | + <div>Before slot content</div> |
| 177 | + </x-slot:before> |
| 178 | +</x-input> |
| 179 | +``` |
| 180 | + |
| 181 | +## After Slot |
| 182 | + |
| 183 | +The `after` slot allows you the flexibility to render any kind of HTML you need to directly after the input element. If you have a trailing addon on the input, it will render after that markup. |
| 184 | + |
| 185 | +```html |
| 186 | +<x-input> |
| 187 | + <x-slot:after> |
| 188 | + <div>After slot content</div> |
| 189 | + </x-slot:after> |
| 190 | +</x-input> |
| 191 | +``` |
| 192 | + |
| 193 | +## Addon Slot Attributes |
| 194 | + |
| 195 | +All types of leading and trailing addons (except the before and after slots) are able to render custom HTML attributes onto the addon markup for you. The only |
| 196 | +requirement is that you use the named slot for the addon, and not the prop. |
| 197 | + |
| 198 | +The following example will add an id and an attribute called data-foo onto the leading addon markup for the input. |
| 199 | + |
| 200 | +```html |
| 201 | +<x-input> |
| 202 | + <x-slot:leading-addon id="hello-world" data-foo="bar"> |
| 203 | + Hello world |
| 204 | + </x-slot:leading-addon> |
| 205 | +</x-input> |
| 206 | +``` |
| 207 | + |
| 208 | +Those attributes will be rendered onto the `<span>` that wraps `Hello world` and **not** onto the input itself. |
0 commit comments