The HTML structure leverages Bootstrap for responsive layout and integrates mermaid.js to render diagrams dynamically. Here’s a simplified version of the HTML:
Textarea: Located in the first column (col-md-6), allows users to input their Mermaid.js code.
Preview Area: In the second column, displays the rendered diagram. Initially shows a placeholder message.
This HTML structure provides a clean and responsive layout for the visualization app. Bootstrap ensures that the app looks good on all screen sizes, while Mermaid.js handles the dynamic rendering of diagrams. In the next sections, we’ll delve into styling with CSS and adding interactivity with JavaScript to make the app fully functional.
Styling with CSS
After setting up the HTML structure using Bootstrap and integrating mermaid.js, the next step is to style the application to ensure it is both visually appealing and user-friendly.
We’ll use custom CSS to enhance the layout and appearance of our visualization app.
We use a media Query (@media (min-width: 768px)) to changes the layout to horizontal (flex-direction: row;) for larger screens, allowing the textarea and preview to sit side by side.
The font used for the text is monospaced, which is ideal for code.
JavaScript for responsiveness
This JavaScript code enhances the visualization app by enabling real-time rendering of mermaid.js diagrams based on user input.
The code attaches an input event listener to the textarea with the ID #code. This listener triggers every time the user types or modifies the Mermaid.js code.
Upon input, the current value of the textarea is retrieved:
the preview area (#preview) is updated with a new <div> containing the mermaid code;
mermaid.init() is called to process and render the diagram within the preview area.
This script ensures that users receive immediate visual feedback as they input their Mermaid.js code, enhancing the interactivity and usability of the visualization tool.
Conclusion
Building a visualization app with Mermaid.js and Bootstrap is an excellent way to integrate dynamic content rendering with responsive design.