mirror of
https://github.com/chase-manning/pokemon-js.git
synced 2025-09-22 22:57:14 +00:00
✨ create text then action component
This commit is contained in:
parent
b5eecb5034
commit
7cc8f6f72f
1 changed files with 58 additions and 0 deletions
58
src/components/TextThenAction.tsx
Normal file
58
src/components/TextThenAction.tsx
Normal file
|
@ -0,0 +1,58 @@
|
|||
import styled from "styled-components";
|
||||
import Frame from "./Frame";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { hideTextThenAction, selectTextThenAction } from "../state/uiSlice";
|
||||
import { useEffect, useState } from "react";
|
||||
import useEvent from "../app/use-event";
|
||||
import { Event } from "../app/emitter";
|
||||
|
||||
const StyledTextThenAction = styled.div`
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 20%;
|
||||
z-index: 100;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
height: 30%;
|
||||
}
|
||||
`;
|
||||
|
||||
const TextThenAction = () => {
|
||||
const dispatch = useDispatch();
|
||||
const textThenAction = useSelector(selectTextThenAction);
|
||||
|
||||
const [textIndex, setTextIndex] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (textThenAction) return;
|
||||
|
||||
setTextIndex(0);
|
||||
}, [textThenAction]);
|
||||
|
||||
useEvent(Event.A, () => {
|
||||
if (!textThenAction) return;
|
||||
|
||||
if (textIndex === textThenAction.text.length - 1) {
|
||||
textThenAction.action();
|
||||
dispatch(hideTextThenAction());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
setTextIndex(textIndex + 1);
|
||||
});
|
||||
|
||||
if (!textThenAction) return null;
|
||||
|
||||
return (
|
||||
<StyledTextThenAction>
|
||||
<Frame wide tall flashing>
|
||||
{textThenAction.text[textIndex]}
|
||||
</Frame>
|
||||
</StyledTextThenAction>
|
||||
);
|
||||
};
|
||||
|
||||
export default TextThenAction;
|
Loading…
Add table
Add a link
Reference in a new issue