Skip to content

fix: apply custom theme colors to embedded chat headers - #8154

Merged
wangdan-fit2cloud merged 1 commit into
v3from
pr@v3@fix_embed_header
Nov 25, 2026
Merged

wangdan-fit2cloud merged 1 commit into
v3from
pr@v3@fix_embed_header

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

fix: apply custom theme colors to embedded chat headers

<el-button text tabindex="-1" class="invisible pointer-events-none ml-1">
<MkIcon name="icon_magnify_outlined" :size="18" />
</el-button>
<el-button text tabindex="-1" class="invisible pointer-events-none">

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my review of the code changes:

Issues & Concerns

1. Repeated expression — extract to a computed property

auth.authProfile?.custom_theme?.header_font_color || 'var(--mk-N900)'

This appears three times. Extract it:

const headerFontColor = computed(
  () => auth.authProfile?.custom_theme?.header_font_color || 'var(--mk-N900)'
)

Benefits: single source of truth, easier maintenance, fewer re-evaluations.

2. Inconsistent fallback handling

  • For MkIcon: falls back to 'var(--mk-N900)'
  • For <h4>: falls back to undefined (relies on inherited/default color)

The h4 will not match the icons when no custom theme is set. Consider using the same fallback (or explicitly deciding the h4 should inherit). This inconsistency looks unintentional.

3. el-button :color prop semantics

Element Plus's color prop on el-button sets the background color by default, not the icon/text color. If the intent is to color the icon, verify this actually works as expected for a text button — otherwise a CSS variable / style on the button may be more reliable. Double-check Element Plus version behavior.

4. Replaced Tailwind arbitrary variant text-N900!

You removed class="text-N900!" in favor of :color. Mixing Tailwind utility classes and Element Plus props for color is inconsistent with the rest of the file (which mostly uses text-N900! style utilities). Consider keeping the styling approach uniform across the header.

5. Store import path convention

import { useStore } from '@/stores/chat'

Other imports in this file use relative paths ('../../message-list/index'). Confirm the @ alias resolves correctly in all build targets (tests, storybook, etc.). Also confirm that importing useStore here doesn't introduce a circular dependency (store → component → store).

6. invisible pointer-events-none ml-1 placeholder buttons

Adding ml-1 to only one of two matching "placeholder" buttons creates asymmetric spacing blind spots. If they are pure placeholders meant to mirror real buttons, keep their classes in sync so future layout alignment stays consistent.

7. Unused shareLink / scope concerns (pre-existing)

Not part of this diff, but note const shareLink = useShareLinkStoreOptional() appears beyond the snippet; ensure it's not dead code.

Optimization Suggestions

  1. Memoize the color via computed (point 1).
  2. Centralize header theming — if multiple header components need header_font_color, consider exposing it via a small composable/store getter rather than accessing auth.authProfile.custom_theme... directly. This decouples components from auth shape.
  3. Type safety: custom_theme?.header_font_color is untyped upstream; confirm the type allows a string; avoid accidentally passing null.
  4. Consider CSS custom property: Set once on the header container, e.g. :style="{ '--header-fg': headerFontColor }", then reference var(--header-fg) in children. Cleaner than repeating per-element.

Correctness Checklist

  • ✅ Optional chaining on authProfile?.custom_theme is safe.
  • ⚠️ Verify useStore() (Pinia?) returns a reactive object; destructuring const { auth } = useStore() can break reactivity if auth is reassigned. Prefer storeToRefs or const store = useStore(); store.auth... unless you've confirmed reactivity is preserved.

Highest-priority fixes: #1 (dedupe), #2 (fallback consistency), #3/#4 (verify el-button :color does what's intended).

@wangdan-fit2cloud
wangdan-fit2cloud merged commit 1fc6166 into v3 Nov 25, 2026
3 of 4 checks passed
@wangdan-fit2cloud
wangdan-fit2cloud deleted the pr@v3@fix_embed_header branch November 25, 2026 07:27
),
dropdown: () => h(MkDropdownMenu, null, { default: renderActions }),
},
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a review of the code with identified irregularities, potential issues, and optimization suggestions.

1. Critical Reactivity Issue (Vue Composition API)

Issue: computed is defined inside setup, which is correct, BUT the expression useViewType() === VIEW_TYPE.EMBED ? useStore().auth : undefined is evaluated once during setup.

  • If the view type changes dynamically or if the store state (auth) changes, the moreIconColor will not update reactively because it depends on a static evaluation of whether to use the store.
  • Specifically, useViewType() is likely a composable returning a Ref. Comparing it directly === inside the ternary condition is wrong if it returns a Ref (you need .value). Even if it returns a boolean, the ternary logic breaks reactivity for the auth part if the branch isn't taken initially.

Recommendation: Make auth reactive properly. Move the conditional logic inside the computed definition.

const moreIconColor = computed(() => {
  // Check if embed AND if auth exists in store
  const isEmbed = useViewType().value === VIEW_TYPE.EMBED; // Assuming useViewType returns a ref
  if (!isEmbed) return undefined;
  
  const store = useStore();
  // Ensure store.auth is reactive. If useStore returns a ref, might need .value depending on implementation.
  return store.auth?.authProfile?.custom_theme?.header_font_color;
});

2. Class Name Specificity & Tailwind/Utility Conflict

Issue:

class: moreIconColor.value ? undefined : 'text-N900!'

When moreIconColor.value is set, you remove the class. However, MkIcon likely applies the color prop via inline styles or a specific class. Mixing dynamic inline/color props with utility classes based on the presence of another dynamic value can lead to flickering or inconsistent styling during reactive updates.

Additionally, text-N900! suggests a custom utility (possibly Tailwind). Ensure that removing it doesn’t leave an inherited color that conflicts with the new color prop.

3. Performance: Repeated useStore() Calls

Issue: In my suggested fix above, calling useStore() inside a computed property is okay, but generally, it’s better to call it once outside.

Optimization:

// Inside setup():
const store = useStore();
const isEmbed = useViewType(); // Assume this is a Ref<boolean> or similar

const moreIconColor = computed(() => {
  if (isEmbed.value !== true) return undefined; // Adjust comparison based on actual return type of useViewType
  return store.auth?.authProfile?.custom_theme?.header_font_color;
});

4. Nullish Coalescing / Optional Chaining Readability

The original code uses long optional chaining: auth?.authProfile?.custom_theme?.header_font_color. This is valid but verbose. Consider destructuring if this theme object is accessed frequently elsewhere. For now, it's acceptable.

5. VNode Construction Safety

In the render function:

h(ElButton, { text: true }, () => h(MkIcon, { name: 'icon_more_outlined', size: 18, color: moreIconColor.value, ... }))

Passing color: undefined explicitly might have side effects depending on how MkIcon handles props (does it treat undefined same as missing?). It is usually cleaner to conditionally apply the prop only if needed, though Vue usually ignores undefined props. Current approach is fine if MkIcon treats undefined color as "use default".

6. Naming Convention

Variable name auth in:

const auth = useViewType() === VIEW_TYPE.EMBED ? useStore().auth : undefined

Is confusing. It holds either the store’s auth object or undefined. A clearer name might be embedThemeColorSource or handling it entirely within the computed as suggested above avoids this intermediate variable which leaks reactivity logic.


✅ Recommended Refactored Code Snippet

import { Fragment, cloneVNode, computed, defineComponent, h, isVNode } from 'vue'
import { ElButton } from 'element-plus'
import type { VNode } from 'vue'
import MkDropdownMenu from '@/components/global/mk-dropdown/MkDropdownMenu.vue'
import MkDropdown from '@/components/global/mk-dropdown/index.vue'
import MkIcon from '@/components/global/mk-icon/index.vue'
import { VIEW_TYPE, useViewType } from '../../../view/index'
import { useStore } from '@/stores/chat'
import { hasRenderableSlotContent } from '@/utils/vnode'

export default defineComponent({
  name: 'HeaderActions',
  props: { collapse: Boolean, alwaysCollapse: Boolean },
  setup(props, { slots }) {
    const store = useStore()
    const viewType = useViewType() 

    // Correctly reactive computed property
    const moreIconColor = computed(() => {
      // Ensure you check . value if useViewType returns a ref
      // If useViewType returns plain boolean, remove .value
      const isEmbed = viewType.value === VIEW_TYPE.EMBED; 
      
      if (!isEmbed) return undefined

      // Safely access custom theme color
      return store.$state.auth?.authProfile?.custom_theme?.header_font_color
    })

    return () => {
      const actions = collectActions(slots.default?.())
      const display = props.alwaysCollapse || (props.collapse && actions.length > 1) ? 'menu' : 'button'
      
      // ... [rest of your logic] ...

      // Inside the h() calls for dropdown trigger:
      /*
       h(ElButton, { text: true }, () =>
        h(MkIcon, {
          name: 'icon_more_outlined',
          size: 18,
          color: moreIconColor.value,
          // Only apply class if color is NOT present
          class: moreIconColor.value ? undefined : 'text-N900!' 
        })
       )
      */
    }
  }
})

Summary of Changes

  1. Fixed Reactivity: Moved conditional logic into computed so changes to View Type or Store update the color dynamically.
  2. Removed Ambiguous Ternary Initialization: Avoided initializing non-reactive variables with potentially reactive data.
  3. Clearer Intent: The code now explicitly checks "Is this Embed?" inside the computed getter, making the dependency graph clearer.

}

/* 对话输入区:边框颜色随内部焦点平滑切换。 */
.mk-conversation-message-input {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary issue in this code is code duplication.

Since .mk-conversation-debug-header and .mk-conversation-embed-header share the exact same background property, you should combine them into a single CSS selector rule to reduce redundancy and improve maintainability:

/* 对话调试和嵌入标题栏:在中性底色上叠加浅主题色。 */
.mk-conversation-debug-header,
.mk-conversation-embed-header {
  background: color-mix(in srgb, var(--mk-primary) 5%, var(--mk-N100));
}

Why this is better:

  1. DRY Principle: If the style needs to change in the future, you only need to update one block instead of two.
  2. Cleaner Comment: You can restore the original combined comment or use a clear shared description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants