Skip to content

Commit 5936fb1

Browse files
committed
fix(Accordion): Use correct content width on larger screens
1 parent 3d05b3f commit 5936fb1

1 file changed

Lines changed: 41 additions & 25 deletions

File tree

src/Startpage/LinkContainer/Accordion/Accordion.tsx

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MouseEvent, PropsWithChildren, useState } from "react"
22

3+
import { css } from "@emotion/react"
34
import styled from "@emotion/styled"
45

56
const StyledAccordionContainer = styled.div`
@@ -12,10 +13,10 @@ export const AccordionContainer = ({ children }: PropsWithChildren) => (
1213
<StyledAccordionContainer>{children}</StyledAccordionContainer>
1314
)
1415

15-
const StyledAccordionGroup = styled.div`
16+
const StyledAccordionGroup = styled.div<{ active: boolean }>`
1617
height: 400px;
1718
display: flex;
18-
flex: 1;
19+
${({ active }) => active && "flex: 1;"}
1920
padding: 0 10px;
2021
flex-direction: row;
2122
border-right: 3px solid var(--default-color);
@@ -24,14 +25,14 @@ const StyledAccordionGroup = styled.div`
2425
}
2526
`
2627

27-
const AccordionContent = styled.div<{ width: number | string | null }>`
28+
const AccordionContent = styled.div`
2829
height: 100%;
29-
width: ${({ width }) => (typeof width !== "number" ? width : `${width}px`)};
30+
flex: 1;
3031
display: flex;
3132
flex-direction: column;
3233
justify-content: center;
3334
overflow: hidden;
34-
transition: 0.3s;
35+
transition: 300ms;
3536
`
3637

3738
const AccordionTitleWrapper = styled.button<{ active: boolean }>`
@@ -108,21 +109,21 @@ const AccordionTitleWrapper = styled.button<{ active: boolean }>`
108109
109110
${({ active }) =>
110111
!active &&
111-
`
112-
:hover{
113-
> * {
114-
color: var(--bg-color);
115-
text-shadow:
116-
5px 0px 0 var(--accent-color),
117-
4px 0px 0 var(--accent-color),
118-
3px 0px 0 var(--accent-color),
119-
2px 0px 0 var(--accent-color),
120-
1px 0px 0 var(--accent-color),
121-
-1px 0px 0 var(--accent-color),
122-
0px 1px 0 var(--accent-color),
123-
0px -1px 0 var(--accent-color);
124-
}
112+
css`
113+
:hover {
114+
> * {
115+
color: var(--bg-color);
116+
text-shadow:
117+
5px 0px 0 var(--accent-color),
118+
4px 0px 0 var(--accent-color),
119+
3px 0px 0 var(--accent-color),
120+
2px 0px 0 var(--accent-color),
121+
1px 0px 0 var(--accent-color),
122+
-1px 0px 0 var(--accent-color),
123+
0px 1px 0 var(--accent-color),
124+
0px -1px 0 var(--accent-color);
125125
}
126+
}
126127
`};
127128
`
128129

@@ -146,12 +147,18 @@ const getAvailableContentWidth = (element: HTMLElement | null) => {
146147
const parent = element?.parentElement
147148
if (!parent) return 0
148149
if (parent.children.length === 1) return "100%"
149-
const barWidth = [...parent.children].reduce(
150-
(width, element) => Math.min(width, (element as HTMLElement).offsetWidth),
151-
Infinity
150+
const accordionHeaderWidth = (() => {
151+
const width = 90
152+
const paddingX = 10 * 2
153+
const border = 3
154+
return width + paddingX + border
155+
})()
156+
const firstBorder = 3
157+
return (
158+
parent.offsetWidth -
159+
firstBorder -
160+
parent.children.length * accordionHeaderWidth
152161
)
153-
console.log(barWidth)
154-
return parent.offsetWidth - parent.children.length * barWidth
155162
}
156163

157164
export const AccordionGroup = ({
@@ -172,6 +179,7 @@ export const AccordionGroup = ({
172179
setContentWidth(0)
173180
}
174181
}}
182+
active={active}
175183
>
176184
<AccordionTitleWrapper
177185
active={active}
@@ -184,7 +192,15 @@ export const AccordionGroup = ({
184192
{title}
185193
</AccordionTitle>
186194
</AccordionTitleWrapper>
187-
<AccordionContent width={contentWidth} aria-hidden={!active || undefined}>
195+
<AccordionContent
196+
style={{
197+
width:
198+
typeof contentWidth === "string"
199+
? contentWidth
200+
: `${contentWidth ?? 0}px`,
201+
}}
202+
aria-hidden={!active || undefined}
203+
>
188204
{children}
189205
</AccordionContent>
190206
</StyledAccordionGroup>

0 commit comments

Comments
 (0)