diff --git a/pages/course/hover-effect/css-descendant-selector.tsx b/pages/course/hover-effect/css-descendant-selector.tsx index 71f9a885..c5e70f2d 100644 --- a/pages/course/hover-effect/css-descendant-selector.tsx +++ b/pages/course/hover-effect/css-descendant-selector.tsx @@ -13,9 +13,6 @@ import { ExerciseDoubleSandbox, } from '@/component/ExerciseDoubleSandbox'; import { ExerciseAccordion } from '@/component/ExerciseAccordion'; -import { Graph9 } from '@/viz/exercise/LollipopFirstSolution/Graph'; -import { Graph12 } from '@/viz/exercise/LollipopHoverEffectSolution/Graph'; -import { Graph22 } from '@/viz/exercise/Hover3CirclesDescandantSelectorIssueSolution/Graph'; const previousURL = '/course/hover-effect/css-pseudo-class'; const currentURL = '/course/hover-effect/css-descendant-selector'; @@ -41,7 +38,7 @@ export default function Home() { title={currentLesson.name} lessonStatus={currentLesson.status} readTime={currentLesson.readTime} - topBadge={'Lesson ' + currentLessonId} + selectedLesson={currentLesson} description={ <>
@@ -164,11 +161,35 @@ export default function Home() {
content:
+
+
+
+); } diff --git a/pages/course/hover-effect/css-pseudo-class.tsx b/pages/course/hover-effect/css-pseudo-class.tsx index 94761b90..52a2fbef 100644 --- a/pages/course/hover-effect/css-pseudo-class.tsx +++ b/pages/course/hover-effect/css-pseudo-class.tsx @@ -14,9 +14,6 @@ import { import { ExerciseAccordion } from '@/component/ExerciseAccordion'; import Link from 'next/link'; import { TakeHome } from '@/component/TakeHome'; -import { Graph9 } from '@/viz/exercise/HoverFirstTreemapSolution/Graph'; -import { Graph10 } from '@/viz/exercise/HoverDeathByStateSolution/Graph'; -import { Graph11 } from '@/viz/exercise/HoverDeathByStateFixSolution/Graph'; const previousURL = '/course/hover-effect/introduction'; const currentURL = '/course/hover-effect/css-pseudo-class'; @@ -42,7 +39,7 @@ export default function Home() { title={currentLesson.name} lessonStatus={currentLesson.status} readTime={currentLesson.readTime} - topBadge={'Lesson ' + currentLessonId} + selectedLesson={currentLesson} description={ <>+++ Note: When you use the
+:hover
pseudo-class on an SVG + area, it activates whenever the mouse enters the entire SVG + rectangle. ++
+
+
++ However, if you apply
+:hover
to ag
{' '} + element, it will only trigger when the mouse hovers over one of the + elements within theg
group! +
The simplest strategy.
diff --git a/pages/course/hover-effect/internal-state.tsx b/pages/course/hover-effect/internal-state.tsx index c3a4182e..ba1b1975 100644 --- a/pages/course/hover-effect/internal-state.tsx +++ b/pages/course/hover-effect/internal-state.tsx @@ -4,7 +4,6 @@ import { LayoutCourse } from '@/component/LayoutCourse'; import { lessonList } from '@/util/lessonList'; import { ChartOrSandbox } from '@/component/ChartOrSandbox'; import { CodeBlock } from '@/component/UI/CodeBlock'; -import { DonutChartHoverDemo } from '@/viz/DonutChartHover/DonutChartHoverDemo'; import { ScatterplotHoverHighlightDimDemo } from '@/viz/ScatterplotHoverHighlightDim/ScatterplotHoverHighlightDimDemo'; import { Badge } from '@/component/UI/badge'; import GraphGallery from '@/component/GraphGallery'; @@ -33,7 +32,7 @@ export default function Home() { title={currentLesson.name} lessonStatus={currentLesson.status} readTime={currentLesson.readTime} - topBadge={'Lesson ' + currentLessonId} + selectedLesson={currentLesson} description={ <>diff --git a/pages/course/hover-effect/introduction.tsx b/pages/course/hover-effect/introduction.tsx index ec79008b..3418d351 100644 --- a/pages/course/hover-effect/introduction.tsx +++ b/pages/course/hover-effect/introduction.tsx @@ -3,8 +3,6 @@ import TitleAndDescription from '@/component/TitleAndDescription'; import { LayoutCourse } from '@/component/LayoutCourse'; import { lessonList } from '@/util/lessonList'; import { Sidenote } from '@/component/SideNote'; -import { ChartOrSandbox } from '@/component/ChartOrSandbox'; -import { ScatterplotHoverHighlightDimDemo } from '@/viz/ScatterplotHoverHighlightDim/ScatterplotHoverHighlightDimDemo'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/component/UI/tabs'; import { ScatterplotHoverHighlightPseudoClassDemo } from '@/viz/ScatterplotHoverHighlightPseudoClass/ScatterplotHoverHighlightPseudoClassDemo'; import { TreemapHoverEffectDemo } from '@/viz/TreemapHoverEffect/TreemapHoverEffectDemo'; diff --git a/pages/course/hover-effect/toggle-class-in-js.tsx b/pages/course/hover-effect/toggle-class-in-js.tsx index f09fc6ed..29bd81b9 100644 --- a/pages/course/hover-effect/toggle-class-in-js.tsx +++ b/pages/course/hover-effect/toggle-class-in-js.tsx @@ -5,6 +5,18 @@ import { lessonList } from '@/util/lessonList'; import { ChartOrSandbox } from '@/component/ChartOrSandbox'; import { CodeBlock } from '@/component/UI/CodeBlock'; import { DonutChartHoverDemo } from '@/viz/DonutChartHover/DonutChartHoverDemo'; +import { Sidenote } from '@/component/SideNote'; +import Link from 'next/link'; +import { Badge } from '@/component/UI/badge'; +import { Caption } from '@/component/UI/Caption'; +import { cn } from '@/util/utils'; +import { buttonVariants } from '@/component/UI/button'; +import { + Exercise, + ExerciseDoubleSandbox, +} from '@/component/ExerciseDoubleSandbox'; +import { ExerciseAccordion } from '@/component/ExerciseAccordion'; +import { Graph23 } from '@/viz/exercise/PieFirstSolution/Graph'; const previousURL = '/course/hover-effect/css-descendant-selector'; const currentURL = '/course/hover-effect/toggle-class-in-js'; @@ -30,75 +42,244 @@ export default function Home() { title={currentLesson.name} lessonStatus={currentLesson.status} readTime={currentLesson.readTime} - topBadge={'Lesson ' + currentLessonId} + selectedLesson={currentLesson} description={ <>
- In the previous lesson, we learned how to modify a hovered graph
- item using the :hover
CSS pseudo-class.
+ In the previous lesson, we explored how to dim elements that are{' '}
+ not being hovered over using a CSS-only approach.
- However, this approach has design limitations. To achieve a - more effective highlighting effect, it's better to simultaneously{' '} - dim the other graph items. -
-- This can be accomplished using CSS alone, with the help of the CSS - descendant selector. + However, there are times when using JavaScript can provide more{' '} + precise control over the hover effect. A handy technique is + to toggle classes with JavaScript. Let’s take a look at how + to do this.
> } /> - -ref
+ - Problem above: when mouse enter the chart area, triggers effect even if - no marker hovered over. + We’ve discussed the{' '} + useRef React hook + a few times now.
- Solution: CSS compound class selecter ( - - MDN doc - - ) + This hook allows us to target specific elements in the DOM and + manipulate them with JavaScript.
+
- In CSS, a compound class selector combines multiple class names to
- target elements that match all of the specified classes.
+ Once we have the containerRef
set up, we can use it to make
+ changes to the SVG container from anywhere in the graph!
- We can use the same css as the above example, but add the highlight
- class using javascript:
+ For example, we can add an onMouseEnter
property to the
+ circle that will apply a hasHighlight
class to the SVG
+ container:
.class1.class2
.
+
+ }
+ />
+
+ We can use CSS{' '}
+
+ compound class selectors
+ {' '}
+ to apply different styles based on whether the{' '}
+ .hasHighlight
class is present!
+
+ For example, we can set the opacity of all "slices" in the container to{' '}
+ 1
, except when the container has the{' '}
+ .hasHighlight
class, in which case the opacity will be set
+ to 0.2
:
+
+ A donut chart is a variation of the more well-known{' '}
+ pie chart. It is easy to create using the{' '}
+ pie()
function from D3.js.
+
+ The following example demonstrates the technique described earlier. When + a slice is hovered over, a class is added to the SVG container, + resulting in a CSS change for all the other slices. +
+
+
The examples below all use this strategy to implement their hover effect.
++ With the SVG and D3 foundations you’ve built, creating a new chart + type becomes a breeze! +
+ > + ), + toDo: ( + <> ++ Descendant selectors are fantastic for creating advanced hover effects + with only CSS! +
+ > + ), + toDo: ( + <> +rowsContainer
to the SVG container and a
+ class row
to each row.
+