Skip to content

Commit ce6b191

Browse files
committed
Overflow the calendar instead of scaling down on narrow screens
Fixes #35
1 parent cbed8ef commit ce6b191

File tree

6 files changed

+60
-29
lines changed

6 files changed

+60
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-activity-calendar",
3-
"version": "2.0.4",
3+
"version": "2.1.0",
44
"description": " A React component to display activity data in calendar",
55
"author": "Jonathan Gruber <gruberjonathan@gmail.com>",
66
"license": "MIT",

src/component/ActivityCalendar.stories.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const meta: Meta<Props> = {
5151
control: { type: 'range', min: 0, max: 20 },
5252
},
5353
blockSize: {
54-
control: { type: 'range', min: 4, max: 100, step: 2 },
54+
control: { type: 'range', min: 4, max: 30, step: 1 },
5555
},
5656
colorScheme: {
5757
control: false,
@@ -258,6 +258,27 @@ export const WithLittleData: Story = {
258258
},
259259
};
260260

261+
export const WithScreenOverflow: Story = {
262+
args: {
263+
data: generateData(),
264+
},
265+
parameters: {
266+
docs: {
267+
source: {
268+
code: '<ActivityCalendar data={data} />',
269+
},
270+
},
271+
},
272+
render: args => (
273+
<Container>
274+
<h1>With screen overflow</h1>
275+
<div style={{ width: 480, border: 'dashed 1px #929292' }}>
276+
<ActivityCalendar {...args} />
277+
</div>
278+
</Container>
279+
),
280+
};
281+
261282
export const WithTooltips: Story = {
262283
args: {
263284
data: generateData(),

src/component/ActivityCalendar.tsx

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
SVGRectEventHandler,
2222
ThemeInput,
2323
} from '../types';
24-
import { generateEmptyData, getClassName, getMonthLabels, groupByWeeks } from '../utils';
24+
import { generateEmptyData, getClassName, getMonthLabels, groupByWeeks } from '../utils/calendar';
2525
import { createTheme } from '../utils/theme';
2626
import styles from './styles.module.css';
2727

@@ -158,7 +158,7 @@ const ActivityCalendar: FunctionComponent<Props> = ({
158158
loading = false,
159159
renderBlock = undefined,
160160
showWeekdayLabels = false,
161-
style = {},
161+
style: styleProp = {},
162162
theme: themeProp = undefined,
163163
totalCount: totalCountProp = undefined,
164164
weekStart = 0, // Sunday
@@ -258,10 +258,7 @@ const ActivityCalendar: FunctionComponent<Props> = ({
258258
: data.reduce((sum, activity) => sum + activity.count, 0);
259259

260260
return (
261-
<footer
262-
className={getClassName('footer', styles.footer)}
263-
style={{ marginTop: 2 * blockMargin, fontSize }}
264-
>
261+
<footer className={getClassName('footer', styles.footer)}>
265262
{/* Placeholder */}
266263
{loading && <div>&nbsp;</div>}
267264

@@ -299,19 +296,15 @@ const ActivityCalendar: FunctionComponent<Props> = ({
299296
}
300297

301298
function renderLabels() {
302-
const style = {
303-
fontSize,
304-
};
305-
306299
if (!showWeekdayLabels && hideMonthLabels) {
307300
return null;
308301
}
309302

310303
return (
311304
<>
312305
{showWeekdayLabels && (
313-
<g className={getClassName('legend-weekday')} style={style}>
314-
{weeks[0].map((day, index) => {
306+
<g className={getClassName('legend-weekday')}>
307+
{weeks[0].map((_, index) => {
315308
if (index % 2 === 0) {
316309
return null;
317310
}
@@ -332,7 +325,7 @@ const ActivityCalendar: FunctionComponent<Props> = ({
332325
</g>
333326
)}
334327
{!hideMonthLabels && (
335-
<g className={getClassName('legend-month')} style={style}>
328+
<g className={getClassName('legend-month')}>
336329
{getMonthLabels(weeks, labels.months).map(({ text, x }, index, labels) => {
337330
// Skip the first month label if there's not enough space to the next one
338331
if (index === 0 && labels[1] && labels[1].x - x <= MIN_DISTANCE_MONTH_LABELS) {
@@ -353,7 +346,8 @@ const ActivityCalendar: FunctionComponent<Props> = ({
353346

354347
const { width, height } = getDimensions();
355348

356-
const calendarStyles = {
349+
const containerStyles = {
350+
fontSize,
357351
[`--${NAMESPACE}-level-0`]: colorScale[0],
358352
[`--${NAMESPACE}-level-1`]: colorScale[1],
359353
[`--${NAMESPACE}-level-2`]: colorScale[2],
@@ -370,16 +364,21 @@ const ActivityCalendar: FunctionComponent<Props> = ({
370364
};
371365

372366
return (
373-
<article className={`${NAMESPACE} ${styles.container}`} style={{ ...style, ...calendarStyles }}>
374-
<svg
375-
width={width}
376-
height={height}
377-
viewBox={`0 0 ${width} ${height}`}
378-
className={getClassName('calendar', styles.calendar)}
379-
>
380-
{!loading && renderLabels()}
381-
{renderCalendar()}
382-
</svg>
367+
<article
368+
className={`${NAMESPACE} ${styles.container}`}
369+
style={{ ...styleProp, ...containerStyles }}
370+
>
371+
<div className={getClassName('scroll-container', styles.scrollContainer)}>
372+
<svg
373+
width={width}
374+
height={height}
375+
viewBox={`0 0 ${width} ${height}`}
376+
className={getClassName('calendar', styles.calendar)}
377+
>
378+
{!loading && renderLabels()}
379+
{renderCalendar()}
380+
</svg>
381+
</div>
383382
{renderFooter()}
384383
</article>
385384
);

src/component/styles.module.css

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
.container {
2+
display: flex;
3+
flex-direction: column;
4+
gap: 8px;
5+
}
6+
17
.container rect {
28
stroke: rgba(0, 0, 0, 0.08);
39
stroke-width: 1px;
@@ -10,11 +16,15 @@
1016
}
1117
}
1218

19+
.scrollContainer {
20+
max-width: 100%;
21+
overflow-x: auto;
22+
overflow-y: hidden;
23+
}
24+
1325
.calendar {
1426
display: block;
15-
max-width: 100%;
16-
height: auto;
17-
overflow: visible;
27+
width: max-content;
1828
}
1929

2030
.calendar text {

src/component/styles.module.css.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export const container: string;
33
export const footer: string;
44
export const legendColors: string;
55
export const loadingAnimation: string;
6+
export const scrollContainer: string;
File renamed without changes.

0 commit comments

Comments
 (0)