Skip to content

Commit

Permalink
⬆️ Update chakra snippets
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammed Hussein Karimi <info@karimi.dev>
  • Loading branch information
mhkarimi1383 committed Dec 15, 2024
1 parent 81fac35 commit 796d480
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/components/ui/close-button.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ButtonProps as ChakraCloseButtonProps } from "@chakra-ui/react"
import type { ButtonProps } from "@chakra-ui/react"
import { IconButton as ChakraIconButton } from "@chakra-ui/react"
import * as React from "react"
import { LuX } from "react-icons/lu"

export interface CloseButtonProps extends ChakraCloseButtonProps {}
export type CloseButtonProps = ButtonProps

export const CloseButton = React.forwardRef<
HTMLButtonElement,
Expand Down
15 changes: 12 additions & 3 deletions src/components/ui/input-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface InputGroupProps extends BoxProps {
startElement?: React.ReactNode
endElement?: React.ReactNode
children: React.ReactElement
startOffset?: InputElementProps["paddingStart"]
endOffset?: InputElementProps["paddingEnd"]
}

export const InputGroup = React.forwardRef<HTMLDivElement, InputGroupProps>(
Expand All @@ -18,19 +20,26 @@ export const InputGroup = React.forwardRef<HTMLDivElement, InputGroupProps>(
endElement,
endElementProps,
children,
startOffset = "6px",
endOffset = "6px",
...rest
} = props

const child =
React.Children.only<React.ReactElement<InputElementProps>>(children)

return (
<Group ref={ref} {...rest}>
{startElement && (
<InputElement pointerEvents="none" {...startElementProps}>
{startElement}
</InputElement>
)}
{React.cloneElement(children, {
...(startElement && { ps: "calc(var(--input-height) - 6px)" }),
...(endElement && { pe: "calc(var(--input-height) - 6px)" }),
{React.cloneElement(child, {
...(startElement && {
ps: `calc(var(--input-height) - ${startOffset})`,
}),
...(endElement && { pe: `calc(var(--input-height) - ${endOffset})` }),
...children.props,
})}
{endElement && (
Expand Down
68 changes: 45 additions & 23 deletions src/components/ui/slider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Slider as ChakraSlider, HStack } from "@chakra-ui/react"
import { Slider as ChakraSlider, For, HStack } from "@chakra-ui/react"
import * as React from "react"

export interface SliderProps extends ChakraSlider.RootProps {
Expand All @@ -22,39 +22,61 @@ export const Slider = React.forwardRef<HTMLDivElement, SliderProps>(
return (
<ChakraSlider.Root ref={ref} thumbAlignment="center" {...rest}>
{label && !showValue && (
<ChakraSlider.Label fontWeight="medium">{label}</ChakraSlider.Label>
<ChakraSlider.Label>{label}</ChakraSlider.Label>
)}
{label && showValue && (
<HStack justify="space-between">
<ChakraSlider.Label fontWeight="medium">{label}</ChakraSlider.Label>
<ChakraSlider.Label>{label}</ChakraSlider.Label>
<ChakraSlider.ValueText />
</HStack>
)}
<ChakraSlider.Control mb={hasMarkLabel ? "4" : undefined}>
<ChakraSlider.Control data-has-mark-label={hasMarkLabel || undefined}>
<ChakraSlider.Track>
<ChakraSlider.Range />
</ChakraSlider.Track>
{value?.map((_, index) => (
<ChakraSlider.Thumb key={index} index={index}>
<ChakraSlider.HiddenInput />
</ChakraSlider.Thumb>
))}
<SliderThumbs value={value} />
<SliderMarks marks={marks} />
</ChakraSlider.Control>
{marks?.length && (
<ChakraSlider.MarkerGroup>
{marks.map((mark, index) => {
const value = typeof mark === "number" ? mark : mark.value
const label = typeof mark === "number" ? undefined : mark.label
return (
<ChakraSlider.Marker key={index} value={value}>
<ChakraSlider.MarkerIndicator />
{label}
</ChakraSlider.Marker>
)
})}
</ChakraSlider.MarkerGroup>
)}
</ChakraSlider.Root>
)
},
)

function SliderThumbs(props: { value?: number[] }) {
const { value } = props
return (
<For each={value}>
{(_, index) => (
<ChakraSlider.Thumb key={index} index={index}>
<ChakraSlider.HiddenInput />
</ChakraSlider.Thumb>
)}
</For>
)
}

interface SliderMarksProps {
marks?: Array<number | { value: number; label: React.ReactNode }>
}

const SliderMarks = React.forwardRef<HTMLDivElement, SliderMarksProps>(
function SliderMarks(props, ref) {
const { marks } = props
if (!marks?.length) return null

return (
<ChakraSlider.MarkerGroup ref={ref}>
{marks.map((mark, index) => {
const value = typeof mark === "number" ? mark : mark.value
const label = typeof mark === "number" ? undefined : mark.label
return (
<ChakraSlider.Marker key={index} value={value}>
<ChakraSlider.MarkerIndicator />
{label}
</ChakraSlider.Marker>
)
})}
</ChakraSlider.MarkerGroup>
)
},
)

0 comments on commit 796d480

Please sign in to comment.