Next.js module css multiple class name - css

I am using Next.js and module.css in a project.
But I have a problem. I have a "button" component inside a component. I want to give both normal className and Style.myClass to that button component. is there a way to do this?
Sorry the question might be a bit complicated, you can understand better with the example below
<Button className={(Style.MyModuleCssClass, "my-global-class")}
size={"large"}
type="primary"> GET MORE
</Button>
can I write a code like this? ie one global class name and one module.css class name

Multiple classname is not a problem, but normally you need to put the classname together like a string operation.
<Button className={`${Style.MyModuleCssClass} my-global-class`} />
Unless you have a library to help you, https://github.com/JedWatson/classnames#readme

Related

How to target CSS module classes after they are hashed?

I'm working in react and have an element that uses CSS modules for styling, like so:
<div className={styles.book__title}>Title: {book.title}</div>
Because class names are hashed, the compiled output is something like:
<div class="book__title_adsfj4">Title: The Lord of the Rings</div>
I've added an event listener on a button that, when pressed, adds the 'title' class to this element to offer additional styling for that specific class, like so:
<div class="book__title_adsfj4 title">Title: The Lord of the Rings</div>
Is there a way where I can either:
append a class to an element and within my css file not have that class be hashed or
add a class to an element that exactly matches the final compiled hash name?
Thanks.
I wrote about the best way I've found to do this here - https://github.com/css-modules/css-modules/issues/199
Basically, it's just importing those styles from the original css file. Because those styles are all hashed the same, no matter if they are imported within different files, the styling will carry over.
When you have some kind of event that triggers a permanent visual change on your element, that means your element has state! And you'll want to use state in React to handle that change.
In React, state is a dictionary on a component that, whenever it changes, triggers a re-render of that component. You can read up on state here: https://reactjs.org/docs/state-and-lifecycle.html
What you'll want to do is add a variable into your state, something like 'buttonPressed' (or whatever you want to call that event or status), and have it be set to true or false depending on if you want the class to be there. When the button is pressed, you'll call this.setState to modify the variable in state and trigger a re-render.
Then, in your render function, you'll simply check what that variable is in your state, and include the class or not, like so:
<div className={styles.book__title + " " + this.state.buttonPressed ? styles.title : ""}>Title: {book.title}</div>

Impossible make a input or AtomTextEditor with React

I'm making a plugin that gets the actual panel or text selection and runs a command on the cli with that value and some params that the user adds in a input.
The idea is to have a similar view than find-and-replace package, but from the first beginning I wasn't able to use space-pane-views for a error on jQuery.
So I decided to make it with React and as far as I was making everything was okayish, but I found 2 big problems.
First I understand what's the View of space-pan and all the ShadowDOM that uses, I feel that is not compatible with React at all, is some kind of big Model that gets data from the dom and from some methods.
So I created a <input /> and I figuret out that you can't interact as normal as a website with that input, doesn't have the hability of delete normally the text and you can't use the atom-text-editor styles into it.
in another hand I try to create a Custom Web Component with React like:
<atom-text-editor
{...this.props}
mini
tabindex='-1'
class={`${this.props.className}`}
data-grammar='text plain null-grammar'
data-encoding='utf8'
/>
and it works with inheriting the styles, but I can't access to the content of the Shadow DOM, neither add eventHandlers like onChange (onKeyPress works btw), this is basically a problem more than React that Atom, but is as far as I went in the intention to create a View in Atom.
Another option could be add draft-js from Fb, but it's a crazy idea for create a simple input.
Any idea to solve one of both problems?
Thanks!
If you add a normal input in React with className='native-key-bindings' the input contains the nativew key bindings, and you can attach the eventHandlers there.

How to add checkmarks and x's when validating in angularjs?

I couldn't find anything in the angularjs docs, nor online, about this specific aspect of form validation. You know when someone writes something in an input field (example: name, phone number, email etc.), and then there is a green checkmark that appears? Or an X that appears implying it's wrong, incomplete etc.
So, I have those images in my folder and ready for use in either situation. Problem is, I can't find the documentation to properly achieve what I would like to achieve. I am thinking that angularjs would be the solution to use, as the rest of my code in is angular.
Since this is angularjs, the only post and documentation that presented a viable option (which does not work for a few reasons) are the following options:
How to put an image in div with CSS?
https://docs.angularjs.org/tutorial/step_09
I was thinking of using CSS to trick the browser into making the one or the other image appear as it validates. I thought it might force the image in my other div to appear, but to no avail.
For example, in this CSS, I tried this:
.ng-valid.ng-dirty .div.test{
border-color: green;
content:url(http://example.com/image);
}
Using this in my HTML:
<div class="test">
<label style="float:left">by:</label>
<input class="form-control controltwo" required ng-model="reviewCtrl.review.author" name="email" id="email" type="email" style="width:350px;" placeholder="Email Address"/>
</div>
As I said before, I am trying to achieve something using angularjs. As CSS can be used for styling, it cannot be tricked into being a styling option and a complex validator. I've tried a few tricks as show on the links, but they don't work. As for the second link, it just isn't made for this purpose, and considering they are made only for filters and images, the docs for the filters don't help a bit.
A simple way of achieving what you want is to look in to the $valid or $invalid properties of your form control.
For example, to show a small message when the email is invalid, you would put this element in your markup.
<div ng-show='reviewForm.email.$dirty && reviewForm.email.$invalid'>Invalid Email</div>
Where reviewForm is the name of your form, and email is the name of your input control.
Here is a plunkr demonstrating this: http://plnkr.co/edit/tUuToy99xjfMhbyMd3eV
You can replace the element with whatever else you want
You can do this with ng-show, ng-src and ng-model depending on what you're validating.
https://docs.angularjs.org/api/ng/directive/ngModel
https://docs.angularjs.org/api/ng/directive/ngShow
https://docs.angularjs.org/api/ng/directive/ngSrc
The example under ng-model:text shows pretty much what you want. If you're not using forms, you should be able to use ng-change to fire off a check and change the image to the appropriate one.
https://docs.angularjs.org/api/ng/input/input%5Btext%5D
If ng-show watches the $valid attribute of the field in question you can hide the check mark when validation is false, and show it when true. You can flip the logic if you want an X.

How would I include an MXML file inline another MXML file?

I would like to include an MXML file in my MXML file in the same way you can include an external file in AS3 using the include directive. Using the include directive brings the code from the external file into the original file at compile time placing it in the same scope.
For example,
Application.mxml:
<Application>
<source="external.mxml"/>
</Application>
External.mxml:
<Styles/>
<Declarations>
<Object id="test"/>
</Declarations>
I need to keep this code/mxml/xml in the external file in scope with the original. Do not ask me why I want to do this.
Another example. Here is my current code (simplified) all in 1 mxml file:
...
File1.mxml
<Button click="clickHandler()"/>
<Script>
public function clickHandler():void {
}
</Script>
...
Here is what I want:
...
File1.mxml
<Group>
<source="File2.mxml"/>
<Button click="clickHandler()"/>
<Group>
File2.mxml
<Script>
public function clickHandler():void {
trace(this); // File1.mxml
}
</Script>
...
I want to split my code out into a separate file...
~~ Update ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Though NOT what I was asking for using a "code behind" scheme achieves partial credit to breaking the code out of the view. So I create a MXML file, MyGroup.mxml or MyGroup.as and that extends Group that contains the clickHandler code.
The problem with this method is that I am then locked to the class type I'm extending, hardcoding the view. So for example I would have to extend Group if the MXML class I want to split into separate files is a Group.
I've worked on projects where this was done and it is not good. People start setting styles and visual aspects or group / view specific properties in the code behind class and later if or when we need to change it or the layout it we have end up with all these dependencies to the container. It becomes a mess. Plus, using Code Behind you can't reuse it (reuse in the way include styles.as is reused). So this is not a solution but thought I'd mention it.
Here is a code behind example,
MyGroupBehind.mxml:
<Group>
<Script>
public function clickHandler():void {
trace(this); // File1.mxml
}
</Script>
</Group>
MyGroupAhead.mxml:
<MyGroupBehind>
<Button click="clickHandler()"/>
</MyGroupBehind>
MXML is converted into a class by the compiler, so there is no way to do what you are asking.
Personally, I think that is a good thing. Breaking things up into separate files does not equal "more organized". In fact I would say it achieves the exact opposite effect. You would do better to focus on a proper component structure, IMHO.
Just start typing the name of your custom component, then press Ctrl+Space. Code completion will populate a list of possible things you might want to add, including the name of your component. Use the down arrow to select your component's name, then press enter. Code completion will set up the namespace and start the tag for your component. If you go to the end of the line and type "/>" (no quotes), voila! you will have an inline tag that represents your custom MXML component.
First of all, any external mxml should be a valid XML. Now, as long as you have a valid MXML file, you simply add it by its name like below:
<Application>
<external:External/>
</Application>
Where 'external' is the namespace for your External.mxml file.
Say my MXML file is called Example in the views folder. Simply call it within the parent MXML file you want this to be in
e.g.
<views:Example/>

css tabs that do not require div changes for the active tab

I'm looking to get ideas on how to not change at all the code used to create css tabs (so that I can place it into an include file to avoid duplicating the code across all files that use it), but my current implementation doesn't allow this because I need to select the active tab using id="selectedTab".
The only implementation I found so far that solves this is the following one:
http://unraveled.com/publications/css_tabs/
It requires assigning a class to each tab and uses the body id to determine the active tab.
Is this the only way or is there any other alternatives?
My current code looks like this (the id=noajax" is used to avoid using ajax to load certain pages):
<div class="productTabsBlock2">
<a id="selectedTab" href="/link1" >OVERVIEW</a>
SCREENSHOTS
<a id="noajax" href="/link3" >SPEED TESTS</a>
<a href="/link4" >AWARDS</a>
</div>
EDIT: asp is available as server side and is already used on these pages.
If you're looking for a non-JS solution, then the body class/id provide the easiest way to do what you want.
If you have access to JS library, you can easily add "selected" class to any of the <a> element and modify its appearance.
Just in case you haven't notice, you can use more than one class definition in an element. For example, <a class="noajax selected" /> is valid and both CSS selectors .noajax and .selected will be applied to the element.
An include file for what? If it's a server side programming language like PHP, you can pass a parameter for the selected tab through various methods.
you could use jQuery to add the `selectedTab' id (or class) like so
$('.productTabsBlock2 a').mouseover(function () {
$(this).addClass('selectedTab');
});

Resources