npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactik

v0.0.4

Published

React library

Downloads

3

Readme

reactik is a react library that contains utilities/helpers for building react apps faster and writing a clean code.

license npm latest package

Get started

Use your preferred package manager:

npm install reactik
yarn add reactik

Documentation

Modals

Implement modal windows in a very clean way without the need of creating states and callbacks for them at every use.

Provide context for modals with ModalProvider component:

import { ModalProvider } from 'reactik';

export const App = () => {
  return (
    <ModalProvider>
      <HomePage />
    </ModalProvider>
  );
};

Create a modal component. In the following example, AlertModal uses Dialog component from Material UI:

import {
  Dialog,
  DialogContent,
  DialogTitle,
  DialogContentText,
  DialogActions,
  Button,
} from '@mui/material';
import { ModalProps } from 'reactik';

type AlertModalData = {
  confirmText: string,
  dismissText: string,
};

type AlertModalResult = 'Y' | 'N';

export const AlertModal: React.FC<
  ModalProps<AlertModalData, AlertModalResult>,
> = ({ modalProps, data, close }) => (
  <Dialog
    open={modalProps.open}
    onClose={() => close()}
    aria-labelledby="alert-dialog-title"
    aria-describedby="alert-dialog-description"
  >
    <DialogTitle id="alert-dialog-title">
      Use Google's location service?
    </DialogTitle>
    <DialogContent>
      <DialogContentText id="alert-dialog-description">
        Let Google help apps determine location. This means sending anonymous
        location data to Google, even when no apps are running.
      </DialogContentText>
    </DialogContent>
    <DialogActions>
      {/* Pass 'Y' or 'N' (AlertModalResult) to `close` function */}
      <Button onClick={() => close('N')}>{data.dismissText}</Button>
      <Button onClick={() => close('Y')} autoFocus>
        {data.confirmText}
      </Button>
    </DialogActions>
  </Dialog>
);

Then use AlertModal:

import { Box, Button } from '@mui/material';
import { useModal } from 'reactik';

import AlertModal from 'path to AlertModal';

export function HomePage() {
  const alertDialog = useModal(AlertModal, {
    // `data` should be type of AlertModalData
    data: {
      confirmText: 'Agree',
      dismissText: 'Disagree',
    },
  });

  const handleAlertOpenRequest = () => {
    // opens the alert modal and waits for result using promise

    // optionally `data` can be passed here, which will override the data
    // passed in `useModal` hook call above
    const data = {
      confirmText: 'Allow',
      dismissText: 'Cancel',
    };

    alertDialog.controls.open(data).then((result) => {
      // `result` here will automatically be typed as AlertModalResult | undefined
      if (result === 'Y') {
        // Clicked Allow
      } else if (result === 'N') {
        // Clicked Cancel
      } else {
        // if undefined, it means modal was dismissed
      }
    });
  };

  return (
    <Box>
      <Button onClick={handleAlertOpenRequest}>
        Request location permissions
      </Button>
    </Box>
  );
}

Services

export interface Todo {
  id: string;
  title: string;
}

export class TodoService {
  async getTodos(): Promise<Todo[]> {
    // resolve and return Todo[]
  }
}
import { createServiceContainer, ServicesProvider } from 'reactik';

import { TodoService } from 'path to TodoService';

// create a service container once.
// services are instantiated at demand,
// when used with serviceContainer.useService hook (see eample below)
export const serviceContainer = createServiceContainer({
  services: {
    todoService: () => new TodoService(),
  },
});

export const App = () => {
  return (
    <ServicesProvider services={serviceContainer.services}>
      <HomePage />
    </ServicesProvider>
  );
};

Then use todoService:

import {
  Box,
  List,
  ListItem,
  ListItemText,
  ListItemAvatar,
  Avatar,
  Typography,
} from '@mui/material';

import { serviceContainer } from 'path to serviceContainer';
import { Todo } from 'path to Todo interface';

export function HomePage() {
  const [todos, setTodos] = useState<Todo[]>([]);

  // service name passed to useService hook is typed,
  // which means you cannot pass any string to it,
  // it will ask you to pass a string that is a key
  // from the services object passed to createServiceContainer above
  const todoService = serviceContainer.useService('todoService');

  useEffect(() => {
    todoService.getTodos().then((items) => {
      setTodos(items);
    });
  }, [todoService]);

  return (
    <Box>
      <Typography variant="h4">Todo list</Typography>
      <List>
        {todos.map((item) => (
          <ListItem key={item.id}>
            <ListItemText>{item.name}</ListItemText>
          </ListItem>
        ))}
      </List>
    </Box>
  );
}