|
| 1 | +import React, { ReactElement } from 'react'; |
| 2 | +import { |
| 3 | + Calendar as BaseCalendar, |
| 4 | + CalendarProps as BaseCalendarProps, |
| 5 | + CalendarCell, |
| 6 | + CalendarGrid as BaseCalendarGrid, |
| 7 | + CalendarGridHeader, |
| 8 | + CalendarGridBody, |
| 9 | + CalendarHeaderCell, |
| 10 | + Heading as BaseHeading, |
| 11 | + DateValue, |
| 12 | + Button as BaseButton |
| 13 | +} from 'react-aria-components'; |
| 14 | +import styled from 'styled-components'; |
| 15 | +import { ChevronLeftIcon, ChevronRightIcon } from '../../../icons'; |
| 16 | +import { getSemanticValue } from '../../../essentials/experimental'; |
| 17 | +import { textStyles } from '../Text/Text'; |
| 18 | +import { get } from '../../../utils/experimental/themeGet'; |
| 19 | + |
| 20 | +const Header = styled.header` |
| 21 | + display: flex; |
| 22 | + align-items: center; |
| 23 | + justify-content: space-between; |
| 24 | + padding-bottom: ${get('space.3')}; |
| 25 | +`; |
| 26 | + |
| 27 | +const Button = styled(BaseButton)` |
| 28 | + appearance: none; |
| 29 | + background: none; |
| 30 | + border: none; |
| 31 | + display: flex; |
| 32 | + cursor: pointer; |
| 33 | + margin: 0; |
| 34 | + padding: 0; |
| 35 | + color: ${getSemanticValue('on-surface')}; |
| 36 | + outline: 0; |
| 37 | +
|
| 38 | + &[data-focused] { |
| 39 | + color: ${getSemanticValue('accent')}; |
| 40 | + } |
| 41 | +
|
| 42 | + &[data-disabled] { |
| 43 | + opacity: 0; |
| 44 | + } |
| 45 | +`; |
| 46 | + |
| 47 | +const Heading = styled(BaseHeading)` |
| 48 | + margin: 0; |
| 49 | + color: ${getSemanticValue('on-surface')}; |
| 50 | + ${textStyles.variants.title2} |
| 51 | +`; |
| 52 | + |
| 53 | +const CalendarGrid = styled(BaseCalendarGrid)` |
| 54 | + border-collapse: collapse; |
| 55 | + border-spacing: 0; |
| 56 | +
|
| 57 | + td { |
| 58 | + padding: 0; |
| 59 | + } |
| 60 | +
|
| 61 | + th { |
| 62 | + padding: 0 0 ${get('space.1')}; |
| 63 | + } |
| 64 | +`; |
| 65 | + |
| 66 | +const WeekDay = styled(CalendarHeaderCell)` |
| 67 | + color: ${getSemanticValue('on-surface')}; |
| 68 | + ${textStyles.variants.label2} |
| 69 | +`; |
| 70 | + |
| 71 | +const Day = styled(CalendarCell)` |
| 72 | + display: flex; |
| 73 | + align-items: center; |
| 74 | + justify-content: center; |
| 75 | + color: ${getSemanticValue('on-surface')}; |
| 76 | + width: 2.5rem; |
| 77 | + height: 2.5rem; |
| 78 | + border-radius: 50%; |
| 79 | + ${textStyles.variants.label2} |
| 80 | + transition: background ease 200ms; |
| 81 | +
|
| 82 | + &[data-focused] { |
| 83 | + outline: ${getSemanticValue('accent')} solid 0.125rem; |
| 84 | + } |
| 85 | +
|
| 86 | + &[data-hovered] { |
| 87 | + cursor: pointer; |
| 88 | + background: ${getSemanticValue('surface-variant')}; |
| 89 | + } |
| 90 | +
|
| 91 | + &[data-selected] { |
| 92 | + outline: 0; |
| 93 | + background: ${getSemanticValue('interactive-container')}; |
| 94 | + color: ${getSemanticValue('on-interactive-container')}; |
| 95 | + } |
| 96 | +
|
| 97 | + &[data-disabled] { |
| 98 | + opacity: 0.38; |
| 99 | + } |
| 100 | +
|
| 101 | + &[data-outside-month] { |
| 102 | + opacity: 0; |
| 103 | + } |
| 104 | +`; |
| 105 | + |
| 106 | +type CalendarProps<T extends DateValue> = BaseCalendarProps<T>; |
| 107 | + |
| 108 | +function Calendar<T extends DateValue>(props: CalendarProps<T>): ReactElement { |
| 109 | + return ( |
| 110 | + <BaseCalendar {...props}> |
| 111 | + <Header> |
| 112 | + <Button slot="previous"> |
| 113 | + <ChevronLeftIcon size={24} /> |
| 114 | + </Button> |
| 115 | + <Heading /> |
| 116 | + <Button slot="next"> |
| 117 | + <ChevronRightIcon size={24} /> |
| 118 | + </Button> |
| 119 | + </Header> |
| 120 | + <CalendarGrid weekdayStyle="short"> |
| 121 | + <CalendarGridHeader>{weekDay => <WeekDay>{weekDay}</WeekDay>}</CalendarGridHeader> |
| 122 | + <CalendarGridBody> |
| 123 | + {date => ( |
| 124 | + <Day date={date}> |
| 125 | + {({ formattedDate }) => (formattedDate.length > 1 ? formattedDate : `0${formattedDate}`)} |
| 126 | + </Day> |
| 127 | + )} |
| 128 | + </CalendarGridBody> |
| 129 | + </CalendarGrid> |
| 130 | + </BaseCalendar> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +export { Calendar }; |
0 commit comments