so I'm using some of Livekit's react components li...
# ask-ai
b
so I'm using some of Livekit's react components like
BarVisualizer
. I want to update their style to match the rest of the website. I'm using TailwindCSS v4. What are the best practices to do that?
t
Thanks for reaching out, mateuszkasprowicz. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you style the
BarVisualizer
component using TailwindCSS! Here are some friendly tips to guide you through the process: 1. Import Required Styles: First, make sure to import the base LiveKit component styles into your React app like this:
import '@livekit/components-styles';
2. Apply Tailwind Classes: The
BarVisualizer
creates bars with the class
.lk-audio-bar
, and when a bar is active, it gets the
data-lk-highlighted="true"
attribute. You can easily style these using Tailwind utilities:
Copy code
.agent-visualizer {
  @apply flex justify-center items-center gap-1 p-2;
  border-radius: 8px;
}

.agent-visualizer > .lk-audio-bar {
  @apply w-3 bg-gray-300;
}

.agent-visualizer > .lk-audio-bar[data-lk-highlighted="true"] {
  @apply bg-purple-500;
}
3. Use CSS Variables: To keep things neat and customizable, try defining global appearance with CSS variables like these:
Copy code
:root {
  --lk-fg: #ff40ff; /* Foreground (active bars) color */
  --lk-va-bg: #333; /* Background (idle bars) color */
}
4. Custom Bar Template: If you want full control, you can provide a custom template using
children
inside
BarVisualizer
. Highlighted bars will have the
data-lk-highlighted
attribute, so you can style them with Tailwind too:
Copy code
<BarVisualizer barCount={7} className="agent-visualizer">
  <div className="w-3 bg-gray-200 rounded-sm" data-lk-highlighted="false" />
</BarVisualizer>
5. Override Defaults: Sometimes LiveKit applies inline styles, but you can override them by using
!important
in your custom styles:
Copy code
.lk-audio-bar[data-lk-highlighted="true"] {
  @apply bg-gradient-to-t from-pink-500 to-purple-400 !important;
}
If you'd like to explore more, the LiveKit React Component documentation is a great resource. Feel free to reach out if you have any questions along the way — I’m here to help! Sources: How do I configure BarVisualizer in React? | complex-agents/nutrition-assistant/nutrition-assistant-frontend/app/globals... | Web and mobile frontends | LiveKit Docs
b
is there a way to style the component directly? Let's say I want to use
BarVisualizer
in two places with completely different design - how to do it efficiently?
t
To style the
BarVisualizer
differently in multiple places, here are some friendly approaches you can try: 1. CSS Classes: You can give each
BarVisualizer
a unique class and then define styles for those classes. For example:
Copy code
.visualizer-one .lk-audio-bar {
  @apply bg-blue-500;
}

.visualizer-two .lk-audio-bar {
  @apply bg-green-500;
}
2. CSS Variables: Another great way is to use CSS custom properties to set styles for each component or section. For example:
Copy code
.visualizer-one {
  --lk-fg: #ff0000; /* Active bar color */
  --lk-va-bg: #333333; /* Idle bar color */
}

.visualizer-two {
  --lk-fg: #00ff00;
  --lk-va-bg: #111111;
}
3. Custom Bar Template: If you want full control over the bar design, you can provide a custom child element to
BarVisualizer
. Here’s how:
Copy code
<BarVisualizer className="visualizer-one">
  <div className="custom-bar" data-lk-highlighted="false" />
</BarVisualizer>

<BarVisualizer className="visualizer-two">
  <div className="custom-bar" data-lk-highlighted="true" />
</BarVisualizer>
4. React Native: If you’re working with React Native, you can customize each instance using the
style
prop for container styling and the
options
prop for bar-specific properties like width, color, and height. These methods should help you style
BarVisualizer
differently across various places with ease. If you’d like, I’m here to help with more details or examples—just let me know! Sources: Community & Support | livekit/livekit_composite | DeepWiki | How do I configure BarVisualizer in React? | docs/functions/BarVisualizer.html | app/assistant/ui/AgentVisualization.tsx
b
for some reason with these audio bars design
Copy code
/* Interview variant - prominent agent voice visualizer */
    .visualizer-interview {
        --visualizer-bar-idle: rgba(255, 255, 255, 0.2);
        --visualizer-bar-active: linear-gradient(
            180deg,
            rgba(34, 211, 238, 0.9) 0%,
            rgba(6, 182, 212, 0.9) 100%
        );
        --visualizer-bar-shadow: 0 0 12px rgba(34, 211, 238, 0.6),
            0 0 6px rgba(255, 255, 255, 0.2);
    }

    /* Compact variant - simple microphone visualizer for control bar */
    .visualizer-compact {
        --visualizer-bar-idle: rgba(255, 255, 255, 0.15);
        --visualizer-bar-active: rgba(255, 255, 255, 0.6);
        --visualizer-bar-shadow: none;
    }

    .visualizer-compact .lk-audio-bar {
        width: 4px;
        border-radius: 2px;
    }
Copy code
export function AgentControlBar({
    controls,
    saveUserChoices = true,
    onDeviceError,
    ...props
}: AgentControlBarProps) {
    const visibleControls = { microphone: true, disconnect: true, ...controls };

    const localPermissions = useLocalParticipantPermissions();
    const { localParticipant, microphoneTrack } = useLocalParticipant();
    const room = useMaybeRoomContext();
    const { buttonProps: disconnectButtonProps } = useDisconnectButton({
        stopTracks: true,
    });

    const micTrackRef: TrackReferenceOrPlaceholder = React.useMemo(() => {
        return {
          participant: localParticipant,
          source: Track.Source.Microphone,
          publication: microphoneTrack,
        };
      }, [localParticipant, microphoneTrack]);

    console.log("micTrackRef", micTrackRef);

    // Extract stopTracks to prevent it from being passed to DOM element
    const { stopTracks, ...buttonProps } = disconnectButtonProps;

    if (!localPermissions) {
        visibleControls.microphone = false;
    } else {
        visibleControls.microphone ??= localPermissions.canPublish;
    }

    const { saveAudioInputEnabled, saveAudioInputDeviceId } =
        usePersistentUserChoices({
            preventSave: !saveUserChoices,
        });

    // Use LiveKit's useTrackToggle hook for microphone control
    const {
        buttonProps: micButtonProps,
        enabled: micEnabled,
        pending: micPending,
    } = useTrackToggle({
        source: Track.Source.Microphone,
        onChange: React.useCallback(
            (enabled: boolean, isUserInitiated: boolean) => {
                if (isUserInitiated) {
                    saveAudioInputEnabled(enabled);
                }
            },
            [saveAudioInputEnabled],
        ),
        onDeviceError: React.useCallback(
            (error: Error) => {
                onDeviceError?.({ source: Track.Source.Microphone, error });
                toast.error(
                    "Microphone error. Please check your microphone settings.",
                );
                console.log("Microphone error", error);

                // Auto-fallback to 'default' mic on typical device errors, then retry enabling
                const errorName = (error as any)?.name;
                if (
                    room &&
                    (errorName === "OverconstrainedError" ||
                        errorName === "NotFoundError" ||
                        errorName === "NotReadableError")
                ) {
                    room.switchActiveDevice("audioinput", "default", false)
                        .then(() => {
                            saveAudioInputDeviceId("default");
                            try {
                                room.localParticipant.setMicrophoneEnabled(
                                    true,
                                    {
                                        echoCancellation: true,
                                        noiseSuppression: true,
                                    },
                                    { forceStereo: false },
                                );
                            } catch (retryError) {
                                console.log(
                                    "Retry microphone enable failed",
                                    retryError,
                                );
                            }
                        })
                        .catch((switchError) => {
                            console.log(
                                "Switch to default microphone failed",
                                switchError,
                            );
                        });
                }
            },
            [onDeviceError, room, saveAudioInputDeviceId],
        ),
    });

    const handleDeviceChange = React.useCallback(
        (kind: MediaDeviceKind, deviceId: string) => {
            if (kind === "audioinput") {
                saveAudioInputDeviceId(deviceId);
            }
        },
        [saveAudioInputDeviceId, room],
    );

    return (
        <div
            className={`relative w-[calc(100%+4rem)] -mx-8 overflow-visible ${props.className || ""}`}
            {...props}
        >
            {/* Simple transparent background */}
            <div className="absolute inset-0 bg-white/10 border-t border-white/30" />

            {/* Content */}
            <div className="relative flex items-center justify-between px-12 py-5">
                {/* Left side - Microphone controls */}
                <div className="flex items-center gap-4">
                    {visibleControls.microphone && (
                        <div className="flex items-center gap-2">
                            <Button
                                {...micButtonProps}
                                variant={micEnabled ? "default" : "outline"}
                                size="lg"
                                disabled={micPending}
                                className={`flex items-center gap-3 px-5 py-3 rounded-xl transition-all duration-200 shadow-md backdrop-blur-sm border font-semibold ${
                                    micEnabled
                                        ? "bg-green-500/90 hover:bg-green-600/90 border-green-400/50 text-white shadow-green-500/15 hover:shadow-green-500/25 hover:scale-105"
                                        : "bg-white/15 hover:bg-white/25 border-white/30 text-white shadow-black/10 hover:scale-105"
                                }`}
                            >
                                {micEnabled ? (
                                    <Mic size={18} />
                                ) : (
                                    <MicOff size={18} />
                                )}
                                <div className="flex items-end h-5 w-8">
                                    <BarVisualizer
                                        trackRef={micTrackRef}
                                        barCount={5}
                                        options={{ minHeight: 4, maxHeight: 16 }}
                                        className="visualizer-compact"
                                    />
                                </div>
                            </Button>
                        </div>
                    )}
I don't see the bars
t
Hey there! If the bars in your
BarVisualizer
aren’t showing up, don’t worry—let’s walk through some common causes and easy fixes together: 1. Make Sure the Context is Set Up Correctly: The
BarVisualizer
needs to be inside a
LiveKitRoom
or
RoomContext.Provider
. Here’s a quick example:
Copy code
<LiveKitRoom>
  <BarVisualizer trackRef={audioTrack} />
</LiveKitRoom>
2. Double-Check Your Track Reference: It’s important that the
trackRef
points to a valid, active audio track. For example:
Copy code
const micTrackRef = {
  participant: localParticipant,
  source: Track.Source.Microphone,
  publication: microphoneTrack,
};

<BarVisualizer trackRef={micTrackRef} />
3. Don’t Forget to Import Styles: Make sure you’ve added this import in your component file so the styles show up properly:
import '@livekit/components-styles';
4. Check for Audio Activity: The bars display based on audio volume, so if there’s no sound or the volume is really low, the bars might not appear. Just confirm that your audio track is active and sending data. 5. Take a Look at the DOM and Styling: Sometimes the bars are there but styled to be invisible. You can tweak their look with CSS classes or variables like this:
Copy code
.lk-audio-bar {
  background-color: rgba(255, 255, 255, 0.2);
}

.lk-audio-bar[data-lk-highlighted="true"] {
  background-color: rgba(34, 211, 238, 0.9);
}
6. Try Adjusting the Options: You can set the height of the bars using the
options
prop to better fit your needs:
<BarVisualizer options={{ minHeight: 4, maxHeight: 16 }} />
If you’re still running into trouble, please feel free to share more about your setup—like if you’re using React web or React Native—and any console errors you might be seeing. I’m here to help you get this sorted out! Sources: Web and mobile frontends | LiveKit Docs | packages/react/src/components/participant/ParticipantAudioTile.tsx | How do I configure BarVisualizer in React?