Skip to content

Commit e293674

Browse files
committed
update css
assuming this works, I want to move reader badge css to its own css file as well /* Reader Badge */ .reader-badge { display: inline-flex; align-items: center; gap: 0.5rem; font-size: 0.875rem; color: var(--color-text-muted); background-color: var(--color-bg-alt); padding: 0.25rem 0.5rem; border-radius: var(--radius); border: 1px solid var(--color-border); } .reader-dot { color: var(--color-success); font-size: 1.2em; line-height: 0.5; animation: pulse 2s infinite; } @Keyframes pulse { 0% { opacity: 1; } 50% { opacity: 0.4; } 100% { opacity: 1; } } @using MyBlog.Core.Interfaces @Inject IReaderTrackingService TrackingService @implements IDisposable @if (_count > 0) { <span class="reader-badge" title="Live reader count"> <span class="reader-dot">●</span> @_count people reading </span> } @code { [Parameter] public string Slug { get; set; } = ""; private int _count; protected override void OnInitialized() { if (string.IsNullOrEmpty(Slug)) return; // Join the group and get initial count TrackingService.JoinPost(Slug); _count = TrackingService.GetReaderCount(Slug); // Listen for updates TrackingService.OnCountChanged += HandleCountChanged; } private void HandleCountChanged(string slug, int count) { if (slug == Slug) { _count = count; InvokeAsync(StateHasChanged); } } public void Dispose() { if (!string.IsNullOrEmpty(Slug)) { TrackingService.LeavePost(Slug); TrackingService.OnCountChanged -= HandleCountChanged; } } } This is a perfect candidate for CSS isolation because of that `@keyframes` animation. By moving it to a scoped file, the `pulse` animation name won't conflict with any other animations you might add later in the project. Follow these steps to migrate the code: ### 1. Create the Scoped CSS File Create a new file named **`ReaderBadge.razor.css`** in the same folder as your component and paste the styles there: ```css /* ReaderBadge.razor.css */ .reader-badge { display: inline-flex; align-items: center; gap: 0.5rem; font-size: 0.875rem; color: var(--color-text-muted); background-color: var(--color-bg-alt); padding: 0.25rem 0.5rem; border-radius: var(--radius); border: 1px solid var(--color-border); } .reader-dot { color: var(--color-success); font-size: 1.2em; line-height: 0.5; animation: pulse 2s infinite; } /* Scoped keyframes: Blazor will rename 'pulse' to something unique to prevent collisions with other components */ @Keyframes pulse { 0% { opacity: 1; } 50% { opacity: 0.4; } 100% { opacity: 1; } } ``` ### 2. Verify the Component File Your **`ReaderBadge.razor`** file should now be strictly HTML and C# logic. Ensure it looks like this: ```razor @using MyBlog.Core.Interfaces @Inject IReaderTrackingService TrackingService @implements IDisposable @if (_count > 0) { <span class="reader-badge" title="Live reader count"> <span class="reader-dot">●</span> @_count people reading </span> } @code { [Parameter] public string Slug { get; set; } = ""; private int _count; protected override void OnInitialized() { if (string.IsNullOrEmpty(Slug)) return; TrackingService.JoinPost(Slug); _count = TrackingService.GetReaderCount(Slug); TrackingService.OnCountChanged += HandleCountChanged; } private void HandleCountChanged(string slug, int count) { if (slug == Slug) { _count = count; InvokeAsync(StateHasChanged); } } public void Dispose() { if (!string.IsNullOrEmpty(Slug)) { TrackingService.LeavePost(Slug); TrackingService.OnCountChanged -= HandleCountChanged; } } } ``` --- ### Important Note on Animations When you use `@keyframes` inside an isolated CSS file, Blazor's compiler is smart enough to rename the animation. For example, it might change `pulse` to `pulse-b-123456` in both the `@keyframes` definition and the `animation:` property. This is why isolation is so powerful—you don't have to worry about accidentally "breaking" an animation in a different component with the same name. ### Clean Up `site.css` Don't forget to delete the `/* Reader Badge */` block from your `site.css`. Keeping the global CSS file as thin as possible makes it much easier to maintain as your project grows. **One quick check:** Are you using this badge inside your `PostDetail.razor` page? If so, you'll just call it like `<ReaderBadge Slug="@currentSlug" />`. Would you like me to help you set up the parent component to pass that Slug correctly?
1 parent e3722b0 commit e293674

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Reader Badge */
2+
.reader-badge {
3+
display: inline-flex;
4+
align-items: center;
5+
gap: 0.5rem;
6+
font-size: 0.875rem;
7+
color: var(--color-text-muted);
8+
background-color: var(--color-bg-alt);
9+
padding: 0.25rem 0.5rem;
10+
border-radius: var(--radius);
11+
border: 1px solid var(--color-border);
12+
}
13+
14+
.reader-dot {
15+
color: var(--color-success);
16+
font-size: 1.2em;
17+
line-height: 0.5;
18+
animation: pulse 2s infinite;
19+
}
20+
21+
@keyframes pulse {
22+
0% { opacity: 1; }
23+
50% { opacity: 0.4; }
24+
100% { opacity: 1; }
25+
}

src/MyBlog.Web/wwwroot/css/site.css

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -542,29 +542,3 @@ a:hover {
542542
.share-btn.success {
543543
color: var(--color-success);
544544
}
545-
546-
/* Reader Badge */
547-
.reader-badge {
548-
display: inline-flex;
549-
align-items: center;
550-
gap: 0.5rem;
551-
font-size: 0.875rem;
552-
color: var(--color-text-muted);
553-
background-color: var(--color-bg-alt);
554-
padding: 0.25rem 0.5rem;
555-
border-radius: var(--radius);
556-
border: 1px solid var(--color-border);
557-
}
558-
559-
.reader-dot {
560-
color: var(--color-success);
561-
font-size: 1.2em;
562-
line-height: 0.5;
563-
animation: pulse 2s infinite;
564-
}
565-
566-
@keyframes pulse {
567-
0% { opacity: 1; }
568-
50% { opacity: 0.4; }
569-
100% { opacity: 1; }
570-
}

0 commit comments

Comments
 (0)