|
| 1 | +# Directives |
| 2 | + |
| 3 | +`Vue3` supports using custom directives, here you can find a list of directives from this package. |
| 4 | + |
| 5 | +More information about custom directives in [here](https://vuejs.org/guide/reusability/custom-directives.html#introduction). |
| 6 | + |
| 7 | +## Tooltip |
| 8 | + |
| 9 | +```vue |
| 10 | +<b-card v-b-tooltip="'My title'" /> |
| 11 | +<b-card v-b-tooltip="{title: 'My title'}" /> |
| 12 | +<b-card v-b-tooltip.hover.top="'My title'" /> |
| 13 | +<b-card v-b-tooltip.focus.right="{title: 'My title'}" /> |
| 14 | +``` |
| 15 | + |
| 16 | +A directive is composed by modifiers and a value, like the examples above. |
| 17 | +The anatomy of directive is: v-{name}.{modifier1}.{modifier2}.{etc.}={value}. |
| 18 | + |
| 19 | +So, here we are defining to use the directive "b-tooltip", with different optional modifiers, and an optional value. |
| 20 | + |
| 21 | +### Triggers modifiers |
| 22 | + |
| 23 | +We can define when we want to trigger a tooltip with the following modifiers. |
| 24 | + |
| 25 | +* click |
| 26 | +* hover |
| 27 | +* focus |
| 28 | +* manual |
| 29 | + |
| 30 | +If we don't define any modifier by default is enabled "hover" and "focus". |
| 31 | + |
| 32 | +### Placement modifiers |
| 33 | + |
| 34 | +We can define when we want to placement a tooltip with the following modifiers. |
| 35 | + |
| 36 | +* left |
| 37 | +* right |
| 38 | +* bottom |
| 39 | +* top |
| 40 | + |
| 41 | +If we don't define any modifier by default is enabled "top". |
| 42 | + |
| 43 | +### Value |
| 44 | + |
| 45 | +A tooltip text could be defined using the value. If we remember the anatomy of a custom directive, we can define at a value. |
| 46 | +With a value we can use an object, a string, a function, or an element. |
| 47 | + |
| 48 | +```ts |
| 49 | + /** |
| 50 | + * Default title value if title attribute isn't present. |
| 51 | + * |
| 52 | + * If a function is given, it will be called with its this reference set |
| 53 | + * to the element that the popover is attached to. |
| 54 | + * |
| 55 | + * @default '' |
| 56 | + */ |
| 57 | + title: string | Element | JQuery | ((this: HTMLElement) => string | Element | JQuery); |
| 58 | +``` |
| 59 | + |
| 60 | +If we want to use an object, we should use the following interface: |
| 61 | + |
| 62 | +```ts |
| 63 | +interface ValueObject { |
| 64 | + delay?: number; // default: 0 |
| 65 | + placement?: 'auto' | 'top' | 'bottom' | 'left' | 'right'; |
| 66 | + title?: string; |
| 67 | + trigger?: | 'click' |
| 68 | + | 'hover' |
| 69 | + | 'focus' |
| 70 | + | 'manual' |
| 71 | + | 'click hover' |
| 72 | + | 'click focus' |
| 73 | + | 'hover focus' |
| 74 | + | 'click hover focus'; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +#### Delay |
| 80 | + |
| 81 | +We can define a delay to display the tooltip, in that case we need to use the object value format, if not by default it's used 0. |
| 82 | + |
| 83 | +#### Pitfalls |
| 84 | + |
| 85 | +When we are using a directive, we have two ways to define the title to use in the tooltip. |
| 86 | + |
| 87 | +##### Incorrect use |
| 88 | + |
| 89 | +````vue |
| 90 | +<b-card v-b-tooltip.hover.top title="my title" /> |
| 91 | +```` |
| 92 | +* First example it's using the property from BCard "title", this property is going to render something like: |
| 93 | + |
| 94 | +```html |
| 95 | +<div class="card"> |
| 96 | + <div/> // header |
| 97 | + <div title="my title"> |
| 98 | + //something here |
| 99 | + </div> |
| 100 | + </div> // footer |
| 101 | +</div> |
| 102 | +``` |
| 103 | + |
| 104 | +Where our title is going to be attached to a child element, but the custom directive is attached to our parent div with class "card". |
| 105 | +So, it's not going to work, and we are going to see a warning in the developer's console. |
| 106 | + |
| 107 | +````vue |
| 108 | +<b-card v-b-tooltip.hover.top="my title" /> |
| 109 | +```` |
| 110 | + |
| 111 | +Here we are not using a string, because is reading ts or js code. So, we need to set a literal string, a variable, function or so on. |
| 112 | + |
| 113 | +##### Correct use |
| 114 | + |
| 115 | +In that cases is working when the title is created in the root component, like this example: |
| 116 | + |
| 117 | +````vue |
| 118 | +<div v-b-tooltip.hover.top title="my title"> |
| 119 | + //something |
| 120 | +</div> |
| 121 | +```` |
| 122 | + |
| 123 | +```html |
| 124 | +<div class="card" title="my title"> |
| 125 | + //something here |
| 126 | +</div> |
| 127 | +``` |
| 128 | + |
| 129 | +In that case, the directive is detecting the title value, and it's going to be used correctly. |
| 130 | + |
| 131 | +````vue |
| 132 | +<b-card v-b-tooltip.hover.top="'my title'" /> |
| 133 | +```` |
| 134 | + |
| 135 | +We should use the value type when the component is not setting to the root component a title. |
| 136 | +Notice that we should use ts/js code, a variable and so on. |
0 commit comments