Skip to Content
Multi-Platform Content Distribution 🚀 Read more → 
BlogPlasmo Style Import Deep Dive: Mastering data-text for Content Script UI

Plasmo Style Import Deep Dive: Mastering data-text for Content Script UI

by esx_ai

When building browser extensions, styling is one of those topics you can’t avoid. Content Script UI needs to be injected into third-party pages, and style conflicts can be a real headache. Let me walk you through Plasmo’s style import system, particularly that magical data-text prefix.

Core Concepts Explained

Q1: Why use data-text for Content Script UI styles?

A: It’s all about style isolation. Content Script UI lives in other people’s pages, so it needs Shadow DOM encapsulation. data-text imports CSS content as strings, allowing runtime creation of <style> elements that get precisely injected into Shadow DOM without conflicting with page styles.

Q2: What’s the difference between regular import and data-text?

A: The differences are significant:

  • Regular import: CSS gets injected into the page’s <head>, potentially conflicting with existing styles
  • data-text: CSS becomes a string, giving you full control over where and when to inject it

In short: one is passive injection, the other is active control.

Q3: Which approach for popup, options, and other extension pages?

A: Use regular import "./style.css" for these. They run in isolated extension environments, won’t conflict with other pages, and benefit from standard CSS build optimizations.

Q4: What other import prefixes does Plasmo support?

A: Besides data-text, there are several useful ones:

  • data-base64:* - converts to base64 string
  • raw:* - raw file content
  • url:* - gets file URL path

Each has its use cases in different scenarios.

Q5: How does Vue 3 handle styles in Content Script UI?

A: Vue 3 uses the style-raw: prefix to import styles, then uses specialized functions to inject the content into Shadow DOM. This mechanism works well with Vue’s single-file component features.

Q6: Is React’s approach different?

A: Fundamentally similar - both rely on Shadow DOM for isolation. Whether React 17 or 18, the underlying style injection logic is the same, though rendering APIs differ.

Practical Decision Guide

Q7: How to choose the right import method?

A: Remember this simple rule:

  • UI injected into web pages → use data-text
  • Extension’s own pages → use regular import

The key question: does your UI need to coexist with third-party pages?

Q8: How does Plasmo auto-configure this?

A: The framework is quite smart - it automatically detects your UI framework and generates appropriate mount code. Follow the conventions, and it handles the underlying style injection for you.

Q9: Why not use data-text everywhere?

A: While technically possible, using data-text in extension pages loses build optimization benefits like code splitting and caching. Regular import is more efficient in isolated environments.

Q10: What role does Shadow DOM play?

A: Shadow DOM creates an isolated container for your extension UI - styles inside are completely separate from the outside world. This prevents page styles from affecting your UI and vice versa.

My Practical Advice

From real project experience, here’s what matters for effective style imports:

  1. Understand the environment: Know where your UI will run
  2. Choose the right prefix: Don’t hesitate to use data-text when needed
  3. Trust the automation: Plasmo’s detection mechanisms are reliable
  4. Consider performance: Use the appropriate method for each scenario

Final Thoughts

Plasmo’s style import system genuinely makes extension development more manageable. The data-text design in particular elegantly solves style isolation while maintaining development convenience.

If you’re working on browser extensions, I recommend taking time to understand these mechanisms. Using them correctly not only prevents style conflicts but also makes the development experience much smoother. My personal takeaway: no more worrying about extension styles breaking pages or page styles mangling extension UI.

If you have experiences or questions about using Plasmo’s style imports, I’d love to hear from you - feel free to share your thoughts!

Last updated on