<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: zhaoyan</title>
    <description>The latest articles on DEV Community by zhaoyan (@zhaoyan).</description>
    <link>https://dev.to/zhaoyan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3291001%2F826414ae-b576-455d-a211-d3210be4faf3.png</url>
      <title>DEV Community: zhaoyan</title>
      <link>https://dev.to/zhaoyan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhaoyan"/>
    <language>en</language>
    <item>
      <title>HarmonyOS Development in Practice: A Comprehensive Analysis of the Popup Component</title>
      <dc:creator>zhaoyan</dc:creator>
      <pubDate>Tue, 24 Jun 2025 12:51:25 +0000</pubDate>
      <link>https://dev.to/zhaoyan/harmonyos-development-in-practice-a-comprehensive-analysis-of-the-popup-component-2k5o</link>
      <guid>https://dev.to/zhaoyan/harmonyos-development-in-practice-a-comprehensive-analysis-of-the-popup-component-2k5o</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The Perfect Fusion of Flexible Interaction and Dynamic Effects&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In HarmonyOS application development, the popup (Popup) is a core component for user guidance and information reminders. It provides a lightweight, non-intrusive interactive experience through pop-up windows, suitable for scenarios such as screen recording prompts, operation guides, and status feedback. This article, based on official documentation, offers an in-depth analysis of the core features and practical techniques of Popups.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Two Types of Popups and Modal Control
&lt;/h2&gt;

&lt;p&gt;Popups are divided into two categories to meet different scenario requirements:&lt;/p&gt;

&lt;h3&gt;
  
  
  System Popup (&lt;code&gt;PopupOptions&lt;/code&gt;)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Built-in standardized layout; directly displays text information via the &lt;code&gt;message&lt;/code&gt; property.&lt;/li&gt;
&lt;li&gt;Supports adding button interactions: &lt;code&gt;primaryButton&lt;/code&gt; (main button) and &lt;code&gt;secondaryButton&lt;/code&gt; (secondary button), both can bind click events.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;bindPopup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handlePopup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Operation Confirmation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;primaryButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Confirm&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;secondaryButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cancel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Custom Popup (&lt;code&gt;CustomPopupOptions&lt;/code&gt;)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fully customizable UI using the &lt;code&gt;@Builder&lt;/code&gt; constructor, supporting mixed graphics and text as well as complex layouts.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Builder&lt;/span&gt; &lt;span class="nf"&gt;popupBuilder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;Row&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;$r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;app.media.icon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Custom Content&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fontSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Modal Control:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When &lt;code&gt;mask: true&lt;/code&gt; (or a color value), a background overlay locks user operations (modal window).&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;mask: false&lt;/code&gt;, users can interact with elements behind the popup (non-modal window).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Dynamic Interaction and Event Listening
&lt;/h2&gt;

&lt;h3&gt;
  
  
  State Binding and Triggering
&lt;/h3&gt;

&lt;p&gt;Control display state via &lt;code&gt;@State&lt;/code&gt; variables (e.g., &lt;code&gt;show: boolean&lt;/code&gt;). For example, toggle by clicking a button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Show Popup&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handlePopup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handlePopup&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Event Callback
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;onStateChange&lt;/code&gt; to listen for popup state changes (such as automatically resetting state when closed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;onStateChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isVisible&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handlePopup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Advanced Features: Animation and Style Customization
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Animation Effects
&lt;/h3&gt;

&lt;p&gt;Combine multiple animations using the &lt;code&gt;transition&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TransitionEffect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;curve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Curve&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Ease&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Supports combined animations (e.g., opacity + translation):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;TransitionEffect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OPACITY&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;TransitionEffect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;translate&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deep Style Customization
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Background color: &lt;code&gt;popupColor: Color.Red&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Mask: &lt;code&gt;mask: { color: '#33d9d9d9' }&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Position &amp;amp; size:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;placement: Placement.Top&lt;/code&gt; (pop up from top)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;width: 200&lt;/code&gt; (fixed width)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Remove default blur background: &lt;code&gt;backgroundBlurStyle: BlurStyle.NONE&lt;/code&gt;
&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Avoiding Soft Keyboard Overlap and Best Practices
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prevent Obstruction Issues
&lt;/h3&gt;

&lt;p&gt;Set &lt;code&gt;keyboardAvoidMode: KeyboardAvoidMode.DEFAULT&lt;/code&gt; so that the popup will automatically avoid being covered by the soft keyboard.&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage Scenario Recommendations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Text Prompt:&lt;/strong&gt; For non-interactive scenarios, use only the &lt;code&gt;message&lt;/code&gt; to convey information.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Action Confirmation:&lt;/strong&gt; Use popups with buttons to handle user decisions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Guided Tips:&lt;/strong&gt; Combine custom popups with precise placement (e.g., below a button).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The Popup component achieves powerful interaction capabilities through a simple API. Its flexible customization (animations, styles, modal control) and event response mechanisms significantly enhance the user experience of HarmonyOS applications. Developers should focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choosing between modal/non-modal modes to balance user interruption,&lt;/li&gt;
&lt;li&gt;Using animations to enhance visual feedback,&lt;/li&gt;
&lt;li&gt;Leveraging &lt;code&gt;@Builder&lt;/code&gt; for branded design.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mastering these techniques allows you to easily address prompt needs in complex scenarios and create more professional HarmonyOS applications.&lt;/p&gt;

</description>
      <category>harmonyos</category>
      <category>arkts</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
