Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature toggle primitive #553

Merged
merged 26 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a94b1e6
FIX#347 Simplified the state initialization
jindaljyoti Jun 27, 2024
67bf31f
Fix#258 changed the base green color to accent color
jindaljyoti Jun 28, 2024
7bac0c5
FIX#258 changed the base green color to accent color
jindaljyoti Jun 28, 2024
6722af2
Update the button sizes
jindaljyoti Jun 29, 2024
6830fd8
update the button size with data attribute
jindaljyoti Jun 30, 2024
0dc30de
update the button size
jindaljyoti Jul 1, 2024
3169d8b
add file
jindaljyoti Jul 2, 2024
27b6677
all conflict resolved
jindaljyoti Jul 8, 2024
c642bd0
Merge branch 'rad-ui:main' into main
jindaljyoti Jul 11, 2024
4ce321c
Merge branch 'rad-ui:main' into main
jindaljyoti Aug 4, 2024
90852e5
Merge branch 'rad-ui:main' into main
jindaljyoti Sep 21, 2024
b4786f9
Merge branch 'rad-ui:main' into main
jindaljyoti Oct 3, 2024
fe3b0c9
Merge branch 'rad-ui:main' into main
jindaljyoti Oct 12, 2024
50f93ed
Merge branch 'rad-ui:main' into main
jindaljyoti Oct 23, 2024
3eeb310
Merge branch 'rad-ui:main' into main
jindaljyoti Oct 26, 2024
b887d51
Merge branch 'rad-ui:main' into main
jindaljyoti Oct 28, 2024
2d4687f
Merge branch 'rad-ui:main' into main
jindaljyoti Oct 30, 2024
58f70f9
Merge branch 'rad-ui:main' into main
jindaljyoti Nov 4, 2024
57fcc01
add toggle primitive
jindaljyoti Nov 7, 2024
683ed66
add toggle ptimitive
jindaljyoti Nov 11, 2024
b7f6db4
update toggle file
jindaljyoti Nov 11, 2024
9829aeb
Merge branch 'rad-ui:main' into main
jindaljyoti Nov 18, 2024
bb80175
Merge branch 'main' into feature_toggle_primitive
jindaljyoti Nov 18, 2024
1c3cfdc
remove customrootclass and contextprovider
jindaljyoti Nov 18, 2024
2212cc8
remove context values
jindaljyoti Nov 18, 2024
3f49aaf
add event listener
jindaljyoti Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/components/ui/Toggle/Toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type ToggleProps = {
children? : React.ReactNode;
className? : string;
onChange : (isPressed:boolean) => void;

};

const Toggle: React.FC<ToggleProps> = ({
Expand All @@ -25,6 +26,7 @@ const Toggle: React.FC<ToggleProps> = ({
onChange,
...props
}) => {

const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);

const [isPressed, setIsPressed] = useState(pressed || defaultPressed);
Expand All @@ -36,10 +38,11 @@ const Toggle: React.FC<ToggleProps> = ({
};

return (

<ButtonPrimitive
className={`${rootClass}`} onClick={handlePressed}
className={`${rootClass}`} onClick ={handlePressed}
data-state={isPressed ? 'on' : 'off'}
type="button"
type='button'
data-disabled={props.disabled ? '' : undefined}
aria-pressed={pressed} {...props}>
Comment on lines +43 to 47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix accessibility and simplify attribute handling

  1. The aria-pressed attribute should reflect the actual state of the toggle, not just the pressed prop.
  2. The data-disabled implementation can be simplified.
     <ButtonPrimitive
         className={`${rootClass}`} onClick={handlePressed}
         data-state={isPressed ? 'on' : 'off'}
         type='button'
-        data-disabled={props.disabled ? '' : undefined}
-        aria-pressed={pressed}
+        data-disabled={props.disabled || undefined}
+        aria-pressed={isPressed}
         {...props}>
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
className={`${rootClass}`} onClick ={handlePressed}
data-state={isPressed ? 'on' : 'off'}
type="button"
type='button'
data-disabled={props.disabled ? '' : undefined}
aria-pressed={pressed} {...props}>
className={`${rootClass}`} onClick ={handlePressed}
data-state={isPressed ? 'on' : 'off'}
type='button'
data-disabled={props.disabled || undefined}
aria-pressed={isPressed} {...props}>

{children}
Expand Down
2 changes: 2 additions & 0 deletions src/components/ui/ToggleGroup/fragments/ToggleItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ToggleItem = ({ children, value = null, ...props }:any) => {

const type = toggleContext?.type;


const isActive = toggleContext?.activeToggles?.includes(value);

const handleToggleSelect = () => {
Expand All @@ -16,6 +17,7 @@ const ToggleItem = ({ children, value = null, ...props }:any) => {
// For Single Case
if (type === 'single') {
if (isActive) {

toggleContext?.setActiveToggles([]);
return;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createContext } from "react";

interface TogglePrimitiveContextType {
isPressed: boolean | undefined;
handlePressed: () => void;

}
export const TogglePrimitiveContext = createContext<TogglePrimitiveContextType>({} as TogglePrimitiveContextType)
kotAPI marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions src/core/primitives/Toggle/fragments/TogglePrimitiveRoot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { useState } from "react";

export interface TogglePrimitiveRootProps {
defaultPressed? : boolean | false;
pressed: boolean;
children?: React.ReactNode;
className?: string;
onChange : (isPressed:boolean) => void;

}
Comment on lines +3 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix prop types and add documentation

The interface needs improvements for better type safety and documentation:

+/**
+ * Props for the Toggle Primitive component that handles basic toggle functionality
+ */
 export interface TogglePrimitiveRootProps {
-    defaultPressed? : boolean | false;
+    /** Initial pressed state for uncontrolled usage */
+    defaultPressed?: boolean;
+    /** Current pressed state for controlled usage. Takes precedence over defaultPressed */
     pressed: boolean;
+    /** Content to be rendered within the toggle */
     children?: React.ReactNode;
+    /** Additional CSS classes */
     className?: string;
+    /** Callback fired when toggle state changes */
     onChange : (isPressed:boolean) => void;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export interface TogglePrimitiveRootProps {
defaultPressed? : boolean | false;
pressed: boolean;
children?: React.ReactNode;
className?: string;
onChange : (isPressed:boolean) => void;
}
/**
* Props for the Toggle Primitive component that handles basic toggle functionality
*/
export interface TogglePrimitiveRootProps {
/** Initial pressed state for uncontrolled usage */
defaultPressed?: boolean;
/** Current pressed state for controlled usage. Takes precedence over defaultPressed */
pressed: boolean;
/** Content to be rendered within the toggle */
children?: React.ReactNode;
/** Additional CSS classes */
className?: string;
/** Callback fired when toggle state changes */
onChange : (isPressed:boolean) => void;
}

const TogglePrimitiveRoot = ({children,className='',defaultPressed,pressed,onChange,...props}:TogglePrimitiveRootProps) => {
const [isPressed, setIsPressed] = useState(pressed || defaultPressed);
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix state management pattern

The current state management mixes controlled and uncontrolled patterns incorrectly:

-const TogglePrimitiveRoot = ({children,className='',defaultPressed,pressed,onChange,...props}:TogglePrimitiveRootProps) => {
-      const [isPressed, setIsPressed] = useState(pressed || defaultPressed);
+const TogglePrimitiveRoot = ({
+  children,
+  className = '',
+  defaultPressed = false,
+  pressed,
+  onChange,
+  ...props
+}: TogglePrimitiveRootProps) => {
+  const [uncontrolledPressed, setUncontrolledPressed] = useState(defaultPressed);
+  const isPressed = pressed ?? uncontrolledPressed;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const TogglePrimitiveRoot = ({children,className='',defaultPressed,pressed,onChange,...props}:TogglePrimitiveRootProps) => {
const [isPressed, setIsPressed] = useState(pressed || defaultPressed);
const TogglePrimitiveRoot = ({
children,
className = '',
defaultPressed = false,
pressed,
onChange,
...props
}: TogglePrimitiveRootProps) => {
const [uncontrolledPressed, setUncontrolledPressed] = useState(defaultPressed);
const isPressed = pressed ?? uncontrolledPressed;


const handlePressed = () => {
const updatedPressed = !isPressed;
setIsPressed(updatedPressed);
onChange(updatedPressed)
}
Comment on lines +14 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve event handling

The toggle needs proper event handling for both mouse and keyboard interactions:

-   const handlePressed = () => {
+   const handlePressed = (event: React.MouseEvent | React.KeyboardEvent) => {
+     if (
+       event.type === 'keydown' &&
+       (event as React.KeyboardEvent).key !== 'Enter' &&
+       (event as React.KeyboardEvent).key !== ' '
+     ) {
+       return;
+     }
+     event.preventDefault();
      const updatedPressed = !isPressed;
-     setIsPressed(updatedPressed);
+     if (pressed === undefined) {
+       setUncontrolledPressed(updatedPressed);
+     }
      onChange(updatedPressed);
    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const handlePressed = () => {
const updatedPressed = !isPressed;
setIsPressed(updatedPressed);
onChange(updatedPressed)
}
const handlePressed = (event: React.MouseEvent | React.KeyboardEvent) => {
if (
event.type === 'keydown' &&
(event as React.KeyboardEvent).key !== 'Enter' &&
(event as React.KeyboardEvent).key !== ' '
) {
return;
}
event.preventDefault();
const updatedPressed = !isPressed;
if (pressed === undefined) {
setUncontrolledPressed(updatedPressed);
}
onChange(updatedPressed);
}


return <span className={className} onClick={handlePressed} {...props}>{children}</span>

};

export default TogglePrimitiveRoot;
7 changes: 7 additions & 0 deletions src/core/primitives/Toggle/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import TogglePrimitiveRoot from "./fragments/TogglePrimitiveRoot"

const TogglePrimitive = {
Root: TogglePrimitiveRoot,
}

export default TogglePrimitive
19 changes: 19 additions & 0 deletions src/core/primitives/Toggle/stories/TogglePrimitive.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import TogglePrimitive from "../index";
import SandboxEditor from "~/components/tools/SandboxEditor/SandboxEditor";

export default {
title: 'Primitives/TogglePrimitive',
component: TogglePrimitive,
render: (args:any) => <SandboxEditor>

<TogglePrimitive.Root {...args}>
</TogglePrimitive.Root>
</SandboxEditor>
}

export const All = {
args: {
className: ''
}
}
Loading