diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d351f2..a2d68ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- **Programmatic Control**: Tooltips can now be controlled programmatically via three methods: + - **Component Template Refs**: Access `show()`, `hide()`, `toggle()`, and `isVisible()` methods via template refs + - **v-model Support**: Two-way binding for tooltip visibility state + - **TooltipControl API**: Global API for controlling directive tooltips by ID (`TooltipControl.show()`, `TooltipControl.hide()`, `TooltipControl.toggle()`, `TooltipControl.isVisible()`) +- Added `modelValue` prop to Tooltip component for v-model support +- Added `id` prop to support unique identification for directive tooltips +- Exported `TooltipExposed` type for TypeScript template ref typing +- Exported `TooltipControl` API for programmatic directive control +- Added comprehensive documentation for programmatic control at `docs/guide/programmatic-control.md` +- Added examples demonstrating all three control methods in showcase components + ## [1.5.0] - 2025-10-30 ### Added diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 970df08..2bf4b64 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -63,6 +63,7 @@ export default defineConfig({ items: [ { text: 'Component Usage', link: '/guide/component-usage' }, { text: 'Directive Usage', link: '/guide/directive-usage' }, + { text: 'Programmatic Control', link: '/guide/programmatic-control' }, { text: 'Global Configuration', link: '/guide/global-config' }, { text: 'Runtime Configuration', link: '/guide/runtime-config' }, ], diff --git a/docs/api/component.md b/docs/api/component.md index 4a5eecf..e7ab734 100644 --- a/docs/api/component.md +++ b/docs/api/component.md @@ -17,6 +17,79 @@ The Vue Custom Tooltip component provides a comprehensive API for creating and c | `showArrow` | `boolean` | `true` | Show arrow pointer | | `offset` | `number` | `8` | Offset from trigger (px) | | `dark` | `'auto' \| boolean` | `'auto'` | Dark mode behavior | +| `modelValue` | `boolean` | `undefined` | Control visibility with v-model | + +## Exposed Methods + +The Tooltip component exposes methods for programmatic control via template refs: + +| Method | Returns | Description | +|--------|---------|-------------| +| `show()` | `void` | Show the tooltip immediately (bypasses delays) | +| `hide()` | `void` | Hide the tooltip immediately (bypasses delays) | +| `toggle()` | `void` | Toggle the tooltip visibility | +| `isVisible()` | `boolean` | Check if the tooltip is currently visible | + +### Using Template Refs + +```vue + + + +``` + +### Using v-model + +The component supports two-way binding for visibility control: + +```vue + + + +``` ## Slots @@ -69,11 +142,21 @@ The content slot allows you to use rich HTML content in your tooltips: The component is fully typed. You can import the types for use in your TypeScript code: ```ts -import type { TooltipPosition, TooltipProps, TooltipTrigger } from '@borstihd/vue-custom-tooltip' +import type { + TooltipExposed, + TooltipPosition, + TooltipProps, + TooltipTrigger, +} from '@borstihd/vue-custom-tooltip' // Example usage in component props const position: TooltipPosition = 'auto' const trigger: TooltipTrigger = 'both' + +// Example usage for template refs +const tooltipRef = ref(null) +// or +const tooltipRef = useTemplateRef('tooltipRef') ``` ## Best Practices diff --git a/docs/api/directive.md b/docs/api/directive.md index 81db2a7..c8c442a 100644 --- a/docs/api/directive.md +++ b/docs/api/directive.md @@ -183,6 +183,7 @@ For more complex configurations, you can pass an object instead of a simple stri | `showArrow` | `boolean` | `true` | Show arrow pointer | | `offset` | `number` | `8` | Offset from trigger (px) | | `dark` | `'auto' \| boolean` | `'auto'` | Dark mode behavior | +| `id` | `string` | `undefined` | Unique identifier for programmatic control | ### Combining with Modifiers @@ -206,6 +207,98 @@ Object configuration can still be combined with modifiers for quick overrides: When using both modifiers and object configuration, modifiers take precedence over the object configuration for the same property. ::: +## Programmatic Control + +Directive tooltips can be controlled programmatically using the `TooltipControl` API. This requires assigning a unique `id` to the tooltip. + +### Using TooltipControl API + +```vue + + + +``` + +### TooltipControl API + +| Method | Parameters | Returns | Description | +|--------|------------|---------|-------------| +| `show(id)` | `id: string` | `void` | Show tooltip by ID | +| `hide(id)` | `id: string` | `void` | Hide tooltip by ID | +| `toggle(id)` | `id: string` | `void` | Toggle tooltip by ID | +| `isVisible(id)` | `id: string` | `boolean` | Check if tooltip is visible | + +::: tip Note +Programmatic control methods (`show`, `hide`, `toggle`) bypass delays for immediate response. +::: + +### Coordinated Tooltips + +You can control multiple tooltips simultaneously: + +```vue + + + +``` + ## Best Practices 1. **Choose Wisely**: Use the directive for simple text tooltips. For rich content, use the component approach. @@ -213,3 +306,4 @@ When using both modifiers and object configuration, modifiers take precedence ov 3. **Concise Content**: Keep tooltip text short and informative. 4. **Position Awareness**: Consider using `.auto` for dynamic positioning. 5. **Animation Speed**: Use `.slow` for important information, `.fast` for subtle hints. +6. **Programmatic IDs**: Use descriptive, unique IDs for programmatically controlled tooltips. diff --git a/docs/guide/programmatic-control.md b/docs/guide/programmatic-control.md new file mode 100644 index 0000000..eaed04e --- /dev/null +++ b/docs/guide/programmatic-control.md @@ -0,0 +1,247 @@ +# Programmatic Control + +You can control tooltips programmatically using several methods: + +## Component Mode + +### Using Template Refs + +Access the component instance using template refs to call methods directly: + +```vue + + + +``` + +### Using v-model + +Control tooltip visibility with two-way binding: + +```vue + + + +``` + +## Directive Mode + +### Using TooltipControl API + +Control directive tooltips by assigning an ID and using the `TooltipControl` API: + +```vue + + + +``` + +## API Reference + +### TooltipExposed (Component) + +Methods exposed by the Tooltip component via template refs: + +| Method | Description | +|--------|-------------| +| `show()` | Show the tooltip immediately (bypasses delays) | +| `hide()` | Hide the tooltip immediately (bypasses delays) | +| `toggle()` | Toggle the tooltip visibility | +| `isVisible()` | Returns `true` if tooltip is currently visible | + +### TooltipControl (Directive) + +Global API for controlling directive tooltips: + +| Method | Parameters | Description | +|--------|------------|-------------| +| `show(id)` | `id: string` | Show tooltip by ID | +| `hide(id)` | `id: string` | Hide tooltip by ID | +| `toggle(id)` | `id: string` | Toggle tooltip by ID | +| `isVisible(id)` | `id: string` | Check if tooltip is visible | + +### v-model Support + +The Tooltip component supports `v-model` for two-way binding of visibility state: + +```vue + +``` + +When the tooltip visibility changes (through user interaction or programmatic control), the bound variable is automatically updated. + +## Use Cases + +### Show Tooltip on Page Load + +```vue + + + +``` + +### Conditional Tooltip Display + +```vue + + + +``` + +### Coordinated Tooltips + +```vue + + + +``` + +## Notes + +- Programmatic `show()` and `hide()` methods bypass the configured delays for immediate display/hiding +- When using `v-model`, the tooltip will still respect trigger events (hover, focus, etc.) but the visibility state will be synchronized +- Directive tooltips require an explicit `id` prop for programmatic control +- Component IDs are optional but can be useful for debugging or advanced use cases diff --git a/package.json b/package.json index d0f5d4f..7755300 100644 --- a/package.json +++ b/package.json @@ -90,4 +90,4 @@ "vitest": "^4.0.8", "vue-tsc": "^3.1.3" } -} \ No newline at end of file +} diff --git a/src/App.vue b/src/App.vue index 148d061..dd33a3d 100644 --- a/src/App.vue +++ b/src/App.vue @@ -4,11 +4,12 @@ import Button from '@/components/Button.vue' import PresetSwitcher from '@/components/PresetSwitcher.vue' import DirectiveBenchmark from '@/components/showcase/DirectiveBenchmark.vue' import DirectiveExample from '@/components/showcase/DirectiveExample.vue' +import ProgrammaticControl from '@/components/showcase/ProgrammaticControl.vue' import TooltipExample from '@/components/showcase/TooltipExample.vue' import ThemeToggle from '@/components/ThemeToggle.vue' import packageJson from '../package.json' -type Tabs = 'component' | 'directive' | 'directive-benchmark' +type Tabs = 'component' | 'directive' | 'directive-benchmark' | 'programmatic-control' const activeTab = ref('component') @@ -85,6 +86,17 @@ const githubRepo = packageJson.repository.url.replace('.git', '') > Tooltip Directive Benchmark + @@ -147,6 +159,7 @@ const githubRepo = packageJson.repository.url.replace('.git', '') + @@ -184,6 +197,11 @@ const githubRepo = packageJson.repository.url.replace('.git', '') :disabled="activeTab === 'directive-benchmark'" @click="switchTab('directive-benchmark')" /> +