The @govflanders/vl-widget-global-header-types
package provides TypeScript types and interfaces for interacting with the Global Header Widget. This widget is embedded in customer web pages and exposes a client object (window.globalHeaderClient
) that allows for dynamic configuration and interaction with the global header.
By using the types defined in this package, developers can ensure type-safe interactions with the widget while improving their development experience (DX) by utilizing autocompletion and built-in documentation.
Adding a Flanders Global Header to Your Website
You can integrate the Flanders Global Header into your website using one of two methods: the entry script or the embed script. Each serves a different purpose:
1. Using the Entry Script
The entry script should be placed in the <head>
section of your HTML, preferably at the top. This is essential because the globalHeaderClient
object is only available after this script has loaded. With the entry script, the header does not render automatically, so you'll need to call the mount
method to display it.
Example of using the entry script:
<!DOCTYPE html>
<html lang="nl">
<head>
<script src="https://widgets.test-vlaanderen.be/api/v2/widget/9717ae3b-84e8-43b2-a814-e35a2547927f/entry"></script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Global header entry script example</title>
</head>
<body>
<div class="your-website-code">👋 Hello world</div>
<script>
globalHeaderClient.mount();
</script>
</body>
</html>
2. Using the Embed Script
The embed script is meant to be placed directly inside the DOM where you want the header to appear. It automatically renders the header at the script's location in the HTML.
Example of using the embed script:
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Global header embed script example</title>
</head>
<body>
<script src="https://widgets.test-vlaanderen.be/api/v2/widget/9717ae3b-84e8-43b2-a814-e35a2547927f/embed"></script>
<div class="your-website-code">👋 Hello world</div>
</body>
</html>
Summary
- Entry script: Use in the
<head>
, requires manual mounting with globalHeaderClient.mount()
method. - Embed script: Place directly in the DOM, automatically renders the header at the script's location.
Installation
To install the global-header-types
package, use the following command:
npm install -D @govflanders/vl-widget-global-header-types
or
yarn add -D @govflanders/vl-widget-global-header-types
Note: It’s recommended to use the caret (^) symbol in your package.json to stay updated with the latest minor and patch versions.
Usage
This package provides all the types required for working with the Global Header Widget. The widget is available on the global window object as window.globalHeaderClient. You can import the types from global-header-types to use them in your project.
Example of using the types to interact with the widget:
import { GlobalHeaderClient, MainLink } from '@govflanders/vl-widget-global-header-types';
const client: GlobalHeaderClient = window.globalHeaderClient;
const contactLink: MainLink = {
label: 'Contact',
href: '/contact',
};
const feedbackLink: MainLink = {
label: 'Feedback',
href: 'https://example.com/feedback',
isExternal: true,
target: '_blank',
};
client.setMainLinks([
contactLink,
feedbackLink,
]);
API
The window.globalHeaderClient
object offers several methods for interacting with the widget. Each method is typed and returns promises, making asynchronous operations easier to handle. Below is a detailed breakdown of each method, its parameters, and the expected return values.
Checkout the GlobalHeaderClient here to see how to use it.
Migration guide: Upgrading from v4 to v5
If you’re currently using version 4 of the Global Header Widget, please follow this guide to upgrade to version 5. This guide highlights the key changes and steps you need to take to ensure a smooth transition.
1. Remove client library script links
No more client library or polyfill scripts: In version 5, you no longer need to include any additional client library or polyfill script links in your HTML. Access to the window.globalHeaderClient
object is automatically provided when you include either the entry script or the embed script.
Action Required:
- Remove any
<script>
tags that reference client libraries or polyfill libraries for the widget. - Ensure that either the entry script or the embed script is included in your HTML.
2. Eliminate installable packages
Script-based integration only: Version 5 removes the need for installable packages via npm or yarn. All functionalities are now accessible by including script tags directly in your HTML’s <head>
or <body>
section.
Action Required:
- Uninstall any npm or yarn packages related to the widget
- Delete any import or require statements in your JavaScript code that reference the widget.
3. Update Embed script links
Version Update in Embed Links: If you’re using the embed script method to include the widget, you need to update the version number in your script’s src URL from 'v1' to 'v2'. Example here.
4. Update API usage
API Changes: The globalHeaderClient API has been updated in version 5. Methods, their signatures, and usage patterns may have changed. Refer to the API section for detailed information on the new methods and their parameters.
Action Required:
- Review all instances where you interact with globalHeaderClient.
- Update your code to align with the new API methods and their signatures.
- Pay special attention to asynchronous methods that now return promises.
Example:
// Using widgets V4
- vl.widget.client.capture(
- // Verplichte capture functie.
- function(widget) {
- if (widget.getPluginTypeId() === 'global_header') {
- widget.getExtension('contact', 'unsafe').resetServicePoints();
- }
- },
- );
// Using widgets V5
+ globalHeaderClient.contact.resetServicePoints();
Unified Access via Scripts: In version 5, you can access the window.globalHeaderClient
object regardless of whether you use the entry script or the embed script.
Action Required:
- Ensure that you include either the entry script or the embed script in your HTML.
- Remove any additional scripts previously used to access window.globalHeaderClient.
6. Verify Styling and Rendering
After integrating version 5 of the widget into your website, it’s important to thoroughly review its appearance and functionality to ensure everything works as expected. Some styling adjustments might be necessary due to changes in the widget’s design or your site’s CSS.
Action Required:
- Test Across Browsers: Check the widget in all supported browsers to confirm consistent styling and behavior.
- Inspect for Conflicts: Look for any CSS conflicts between the widget and your site’s stylesheets.
- Adjust Styles if Needed: Modify your CSS or override styles to resolve any layout or design issues.
- Seek Assistance: If problems persist, don’t hesitate to contact us for support.
Notes
Language Switching
The widget automatically adapts to the language of the document. It observes the lang
attribute on the <html>
tag, so it's the implementer's responsibility to handle any language switching functionality. If the lang
attribute changes, the widget will update to display the correct language.
Supported languages:
- nl (Dutch)
- fr (French)
- de (German)
- en (English)
To change the document language, you can use the following JavaScript snippet:
window.document.documentElement.lang = 'nl';
This will switch the document's language to Dutch, and the widget will automatically reflect the change.
Note: When using a multilingual widget, ensure its configuration also supports multiple languages.
Translatable Type
The Translatable<T>
type allows an object to either be a single value or a language-specific dictionary. This is useful when content needs to be available in multiple languages.
Example
For example, setMainLinks
can accept single-language or multi-language links:
const singleLanguageLink: Translatable<MainLink> = {
label: 'Home',
href: 'https://www.example.com',
};
const multiLanguageLink: Translatable<MainLink> = {
nl: {
label: 'Startpagina',
href: 'https://www.example.com/nl',
},
en: {
label: 'Home',
href: 'https://www.example.com/en',
},
fr: {
label: 'Accueil',
href: 'https://www.example.com/fr',
},
de: {
label: 'Startseite',
href: 'https://www.example.com/de',
},
};
client.setMainLinks([singleLanguageLink]);
client.setMainLinks([multiLanguageLink]);
Browser Support
The Global Header Widget supports the two most recent versions of all major browsers:
- Google Chrome
- Mozilla Firefox
- Microsoft Edge
- Safari
Please ensure your users are on the latest versions of these browsers to guarantee full functionality of the widget.
Contact Information
If you need assistance or have any questions regarding the Global Header Widget, feel free to reach out to us:
Our team is here to help with any issues or inquiries you might have.
Maintainers