import CONSTANTS from "../constants/constants"; import { $, Fragment, component$, h, useStore, useStylesScoped$, } from "@builder.io/qwik"; // constants // types // import { DateTime } from '../types/DateTime'; type DateTime = { id: string; year: number; month: number; day: number; dayOfWeek: number; dayDiffFromToday: number; activities: any; level: number; }; // constants // types // import { DateTime } from '../types/DateTime'; type DayProps = { clickHandler: Function; dt: DateTime; color: string; top: string; right: string; // tooltipText style showTooltip: boolean; tooltipBgColor?: string; tooltipTextColor?: string; tooltipText?: string; // tooltipText content activities: any[]; year: number; month: number; day: number; }; export const getDayRight = ({ right, tooltipPosition }) => { return ( "calc(" + right + " + " + (tooltipPosition === "left" ? "-178px" : tooltipPosition === "center" ? "-96px" : "-7px") + ")" ); }; export const getTooltipText = ({ tooltipText, activities, year, month, day, CONSTANTS, }) => { if (!!tooltipText) { return tooltipText .replaceAll("{{numOfActivities}}", activities?.length) .replaceAll("{{year}}", year) .replaceAll("{{month}}", month) .replaceAll("{{day}}", day); } return `${activities?.length} activities on ${CONSTANTS.MONTH_MAP[month]} ${day}, ${year}`; }; export const updateShowTooltip = function updateShowTooltip( props, state, action, event ) { if (event.clientX < 132) { state.tooltipPosition = "left"; } else if (window.innerWidth - event.clientX < 132) { state.tooltipPosition = "right"; } else { state.tooltipPosition = "center"; } state._showTooltip = action === "enter"; }; export const Day = component$((props: DayProps) => { useStylesScoped$(STYLES); const state = useStore({ _showTooltip: false, tooltipPosition: "center", }); return (
props.clickHandler(props.dt))} onMouseEnter$={$((event) => updateShowTooltip(props, state, "enter", event) )} onMouseLeave$={$((event) => updateShowTooltip(props, state, "leave", event) )} style={{ backgroundColor: props.color, top: props.top, right: props.right, }} >
{props.showTooltip && state._showTooltip ? (
{getTooltipText({ tooltipText: props.tooltipText, activities: props.activities, year: props.year, month: props.month, day: props.day, CONSTANTS, })}
) : null}
); }); export default Day; export const STYLES = ` .div-Day { z-index: 10; position: absolute; width: 10px; height: 10px; outline: 1px solid rgba(27, 31, 35, 0.06); outline-offset: -1px; border-radius: 2px; } .div-Day-2 { z-index: 15; position: absolute; width: 178px; padding: 6px 8px; text-align: center; border-radius: 6px; } `;