Component: FormTag
Relevant source files
The following files were used as context for generating this wiki page:The FormTag component is a small, modal-like form used for creating and adding new tags to the application. It is rendered conditionally within the main App component and provides a simple user interface consisting of a text input for the tag name and a submission button.
This component is responsible for capturing user input for a new tag and updating the application's global state using a Zustand store. It utilizes the motion/react library for entry and exit animations, providing a smooth user experience. Its visibility is controlled by its parent component, App.jsx.
Architecture and Rendering
The FormTag component is part of the main application view and is rendered conditionally based on the tagOpen state managed within App.jsx. When the user clicks the "AD" button in the sidebar, the tagOpen state is toggled, which in turn mounts or unmounts the FormTag component. The AnimatePresence component from motion/react wraps FormTag to handle its enter and exit animations.
Sources: src/App.jsx:13, src/App.jsx:49-55, src/App.jsx:92-96
Visibility Control Flow
The following diagram illustrates how the FormTag component's visibility is controlled from the App component.
This diagram shows that the App component manages the tagOpen state. When tagOpen is true, FormTag is rendered. The component can then call the isVisible(false) prop to set tagOpen to false and unmount itself.
Sources: src/App.jsx:49-55, src/App.jsx:94, src/components/Form/FormTag.jsx:8, src/components/Form/FormTag.jsx:11
UI and Styling
The component's root is a motion.div which provides slide-in and fade-in/out animations. The form itself contains a single text input for the tag name and a submit button.
- Animations: The component uses
motion/reactfor animations. It animates in fromx: -100withopacity: 0tox: 0withopacity: 1, and reverses this on exit. - Styling: Styling is handled by Tailwind CSS classes. Custom styles like
rad(for border-radius) and colors likebg-formandtext-textare defined insrc/App.css.
Sources: src/components/Form/FormTag.jsx:14-17, src/components/Form/FormTag.jsx:27-35, src/App.css:5, src/App.css:22-28
Key UI Elements
| Element | Description | Source File |
|---|---|---|
motion.div | The main container that handles enter/exit animations using initial, animate, and exit props. | src/components/Form/FormTag.jsx:14-18 |
<form> | Wraps the input and button. The onSubmit event is handled by handletagSubmit. | src/components/Form/FormTag.jsx:19-22 |
<input> | A text input with the name tn for the tag name. It has a maxLength of 12. | src/components/Form/FormTag.jsx:29-35 |
<button> | The "Add Tag" button of type="submit" which triggers the form submission. | src/components/Form/FormTag.jsx:37-42 |
State Management and Logic
FormTag interacts with a global Zustand store defined in useListStore.js to manage application-wide tags.
Props
| Prop | Type | Description | Source File |
|---|---|---|---|
isVisible | Function | A state setter function passed from App.jsx to control the form's visibility. | src/components/Form/FormTag.jsx:4 |
State Interaction
The component uses the useListStore hook to access the addTag action.
// src/components/Form/FormTag.jsx:5
const addTag = useListStore((state) => state.addTag);
The addTag function in the store ensures that a tag is only added if it doesn't already exist in the tags array.
// src/Hooks/useListStore.js:43-47
addTag: (tag) => {
set((state) => ({
tags: state.tags.includes(tag) ? state.tags : [...state.tags, tag],
}));
},
This state is persisted to a local file (scriptStorage.json) via the persist middleware in Zustand.
Sources: src/Hooks/useListStore.js:31, src/Hooks/useListStore.js:63-70
Form Submission Flow
The handletagSubmit function orchestrates the entire process when the user submits the form.
This sequence diagram shows the steps taken upon form submission:
- The default form action is prevented.
- The tag name is extracted and trimmed from the input field.
- The
addTagaction fromuseListStoreis called to update the global state. - The form is reset.
- The
isVisible(false)prop is called to close the form.
Sources: src/components/Form/FormTag.jsx:7-12, src/Hooks/useListStore.js:43-47
Conclusion
The FormTag component is a simple yet crucial piece of the application's data management system. It provides a straightforward UI for users to add new tags, which are then persisted globally via a Zustand store. Its integration with motion/react for animations and its clear separation of concerns—relying on a parent for visibility control and a global store for state changes—make it a well-encapsulated and reusable component within the Scripty application.
Sources: src/components/Form/FormTag.jsx, src/App.jsx, src/Hooks/useListStore.js