Skip to content

Commit afddcac

Browse files
author
jdv
committed
testing interraction without embedding in provider
1 parent 27c2bed commit afddcac

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

crowdsec-docs/src/components/InteractiveCheckbox.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import React, { useState, useEffect, createContext, useContext } from 'react';
33
// Global context to manage checkbox states across all instances
44
const CheckboxContext = createContext();
55

6-
// Provider component to wrap your MDX content
7-
export const CheckboxProvider = ({ children }) => {
6+
// Hook to create a standalone checkbox manager
7+
export const useCheckboxManager = () => {
88
const [checkboxStates, setCheckboxStates] = useState({});
99
const [manualChecks, setManualChecks] = useState({});
1010

@@ -60,14 +60,21 @@ export const CheckboxProvider = ({ children }) => {
6060
}));
6161
};
6262

63+
return {
64+
checkboxStates,
65+
manualChecks,
66+
updateCheckbox,
67+
registerCheckbox,
68+
isCheckboxChecked
69+
};
70+
};
71+
72+
// Provider component to wrap your MDX content (optional)
73+
export const CheckboxProvider = ({ children }) => {
74+
const checkboxManager = useCheckboxManager();
75+
6376
return (
64-
<CheckboxContext.Provider value={{
65-
checkboxStates,
66-
manualChecks,
67-
updateCheckbox,
68-
registerCheckbox,
69-
isCheckboxChecked
70-
}}>
77+
<CheckboxContext.Provider value={checkboxManager}>
7178
{children}
7279
</CheckboxContext.Provider>
7380
);
@@ -78,13 +85,17 @@ export const InteractiveCheckbox = ({
7885
id,
7986
references = [],
8087
label = '',
81-
className = ''
88+
className = '',
89+
manager = null // Accept a manager prop
8290
}) => {
83-
const context = useContext(CheckboxContext);
91+
const contextManager = useContext(CheckboxContext);
8492

85-
// Fallback if context is not available (useful for development/SSR)
86-
if (!context) {
87-
console.warn('InteractiveCheckbox must be used within a CheckboxProvider');
93+
// Use the passed manager or fall back to context, then to a warning fallback
94+
const checkboxManager = manager || contextManager;
95+
96+
// Fallback if no manager is available
97+
if (!checkboxManager) {
98+
console.warn('InteractiveCheckbox must either be used within a CheckboxProvider or have a manager prop passed to it');
8899
return (
89100
<div className={`inline-flex items-center gap-2 ${className}`}>
90101
<div
@@ -112,7 +123,7 @@ export const InteractiveCheckbox = ({
112123
updateCheckbox,
113124
registerCheckbox,
114125
isCheckboxChecked
115-
} = context;
126+
} = checkboxManager;
116127

117128
useEffect(() => {
118129
if (registerCheckbox) {
@@ -143,7 +154,10 @@ export const InteractiveCheckbox = ({
143154
};
144155

145156
return (
146-
<div className={`inline-flex items-center gap-2 ${className}`}>
157+
<div
158+
className={`inline-flex items-center gap-2 ${className}`}
159+
style={{ display: 'inline-flex', alignItems: 'center', gap: '8px' }}
160+
>
147161
<div
148162
onClick={handleClick}
149163
style={{
@@ -156,7 +170,8 @@ export const InteractiveCheckbox = ({
156170
display: 'flex',
157171
alignItems: 'center',
158172
justifyContent: 'center',
159-
transition: 'all 0.2s ease'
173+
transition: 'all 0.2s ease',
174+
flexShrink: 0
160175
}}
161176
>
162177
{isChecked && (
@@ -177,7 +192,14 @@ export const InteractiveCheckbox = ({
177192
{label && (
178193
<label
179194
onClick={handleClick}
180-
style={{ cursor: 'pointer', userSelect: 'none' }}
195+
style={{
196+
cursor: 'pointer',
197+
userSelect: 'none',
198+
margin: 0,
199+
padding: 0,
200+
display: 'inline',
201+
lineHeight: '1.2'
202+
}}
181203
>
182204
{label}
183205
</label>

crowdsec-docs/unversioned/user_guides/interactive_se_install/01_install.mdx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ pagination_next: user_guides/interactive_se_install/se_install_02
77
import Tabs from '@theme/Tabs';
88
import TabItem from '@theme/TabItem';
99
import CodeBlock from '@theme/CodeBlock';
10-
import { CheckboxProvider, InteractiveCheckbox } from '@site/src/components/InteractiveCheckbox.js';
10+
import { useCheckboxManager, InteractiveCheckbox } from '@site/src/components/InteractiveCheckbox.js';
11+
export const checkboxManager = useCheckboxManager();
1112

1213
# Interactive Security Engine Installation Guide
1314

@@ -72,8 +73,7 @@ Let's check that CrowdSec is running and able to retrieve the community blocklis
7273

7374
#### Enroll your Security Engine into CrowdSec Console
7475
For advanced monitoring and trouble shooting the CrowdSec Console is a great tool to visualize your Security Engine's activity and alerts.
75-
<CheckboxProvider>
76-
<InteractiveCheckbox id="ConsoleEnroll" label=" Enroll into the console " />
76+
<InteractiveCheckbox id="ConsoleEnroll" label=" Enroll into the console " manager={checkboxManager} />
7777

7878
- [link to doc]
7979
- You'll see a confirmation pop up in the console for enrollment if not you might have conectivity issue to the central API [link to troubleshooting section]
@@ -83,8 +83,7 @@ For advanced monitoring and trouble shooting the CrowdSec Console is a great too
8383

8484
#### CrowdSec installation health
8585

86-
<InteractiveCheckbox id="CrowdsecRunning" label="Check that the CrowdSec service is running" references={["ConsoleEnroll"]} />
87-
</CheckboxProvider>
86+
<InteractiveCheckbox id="CrowdsecRunning" label="Check that the CrowdSec service is running" references={["ConsoleEnroll"]} manager={checkboxManager} />
8887
```bash
8988
systemctl status crowdsec
9089
```

0 commit comments

Comments
 (0)