File tree Expand file tree Collapse file tree 4 files changed +63
-38
lines changed
src/components/CustomTags Expand file tree Collapse file tree 4 files changed +63
-38
lines changed Original file line number Diff line number Diff line change 11import styles from "./styles.module.scss" ;
22import { ReactNode , useContext , useEffect } from "react" ;
33import { CustomTagsContext } from "./CustomTagsContext" ;
4+ import { CustomTagsFilterList } from "./CustomTagsFilterList" ;
45
56export interface CustomTagsContainerProps {
67 tags : string ; // comma-separated values
@@ -31,6 +32,8 @@ export default function CustomTagsContainer({
3132 }
3233 >
3334 { children }
35+
36+ < CustomTagsFilterList tags = { tagList } />
3437 </ div >
3538 ) ;
3639}
Original file line number Diff line number Diff line change 1+ import { CSSProperties , useContext } from "react" ;
2+ import styles from "./styles.module.scss" ;
3+ import { CustomTagsContext } from "./CustomTagsContext" ;
4+
5+ interface CustomTagsFilterListProps {
6+ tags : string [ ] ;
7+ style ?: CSSProperties ;
8+ subtitle ?: string ;
9+ }
10+
11+ export function CustomTagsFilterList ( {
12+ tags,
13+ style,
14+ subtitle,
15+ } : CustomTagsFilterListProps ) {
16+ const [ { filter } , setState ] = useContext ( CustomTagsContext ) ;
17+
18+ if ( tags . length < 1 ) return null ;
19+
20+ const onClickFilter = ( update : string ) => {
21+ if ( filter === update ) update = undefined ; // toggle off
22+
23+ setState ( ( state ) => ( { ...state , filter : update } ) ) ;
24+
25+ const article = document . querySelector ( "article" ) ;
26+
27+ if ( article ) {
28+ article . classList [ update ? "add" : "remove" ] (
29+ styles [ "custom-tag-toggled" ] ,
30+ ) ;
31+ }
32+ } ;
33+
34+ return (
35+ < div className = { styles [ "custom-tags-filters" ] } style = { style } >
36+ { subtitle && < b > { subtitle } </ b > }
37+
38+ < ul >
39+ { tags . map ( ( tag ) => (
40+ < li
41+ key = { tag }
42+ className = { filter === tag ? styles [ "custom-tags-active" ] : "" }
43+ >
44+ < button onClick = { ( ) => onClickFilter ( tag ) } > { tag } </ button >
45+ </ li >
46+ ) ) }
47+ </ ul >
48+ </ div >
49+ ) ;
50+ }
Original file line number Diff line number Diff line change 11import { useContext } from "react" ;
2- import styles from "./styles.module.scss" ;
32import { CustomTagsContext } from "./CustomTagsContext" ;
3+ import { CustomTagsFilterList } from "./CustomTagsFilterList" ;
44
55export default function CustomTagsFilters ( ) {
6- const [ { tags, filter } , setState ] = useContext ( CustomTagsContext ) ;
6+ const [ { tags } ] = useContext ( CustomTagsContext ) ;
77
8- if ( tags . length < 1 ) return null ;
9-
10- const onClickFilter = ( update : string ) => {
11- if ( filter === update ) update = undefined ; // toggle off
12-
13- setState ( ( state ) => ( { ...state , filter : update } ) ) ;
14-
15- const article = document . querySelector ( "article" ) ;
16-
17- if ( article ) {
18- article . classList [ update ? "add" : "remove" ] (
19- styles [ "custom-tag-toggled" ] ,
20- ) ;
21- }
22- } ;
23-
24- return (
25- < div className = { styles [ "custom-tags-filters" ] } >
26- < b > Tags: </ b >
27-
28- < ul >
29- { tags . map ( ( tag ) => (
30- < li
31- key = { tag }
32- className = { filter === tag ? styles [ "custom-tags-active" ] : "" }
33- >
34- < button onClick = { ( ) => onClickFilter ( tag ) } > { tag } </ button >
35- </ li >
36- ) ) }
37- </ ul >
38- </ div >
39- ) ;
8+ return < CustomTagsFilterList tags = { tags } subtitle = "Tags: " /> ;
409}
Original file line number Diff line number Diff line change 22 display : flex ;
33 flex-flow : row ;
44 gap : 0.5em ;
5- margin-bottom : 1.5em ;
5+ margin : 0 0 1.5em 0 ;
6+ padding : 0 ;
67
78 ul {
8- margin : 0 0 1 em 0 ;
9+ margin : 0 ;
910 padding : 0 ;
1011 display : flex ;
1112 flex-flow : row ;
4647}
4748
4849.custom-tag-toggled {
49- // all siblings after first paragraph
50+ // all siblings after first paragraph, except for tags and filtered container
5051 p :first-of-type
51- ~ * :not (.custom-tags-filters ):not (.custom-tags-container-visible ) {
52+ ~ * :not (.custom-tags-filters ):not (.custom-tags-container-visible ):not (
53+ .custom-tags-container-visible *
54+ ) {
5255 display : none ;
5356 }
5457}
You can’t perform that action at this time.
0 commit comments